快速(可能是愚蠢的)問題。這是我的網站的流程:用戶登錄並重定向到自定義管理頁面。在此管理頁面上,他們有能力創建「個人資料」。我想將他們創建的配置文件與其用戶數據相關聯,以使1個用戶與1個配置文件關聯。連接到Django的用戶模型
出於某種原因,下面不工作(無非是想
UserAdmin.Models
from django.db import models
from django.contrib.auth.models import User
class Profile(models.Model):
username = models.ForeignKey(User)
firstname = models.CharField(max_length=200)
lastname = models.CharField(max_length=200)
email = models.EmailField(max_length=200)
def __unicode__(self):
return self.username
UserAdmin.Views
def createprofile(request):
user = User.objects.get(id=1)
profile = Profile(username=user, firstname='Joe', lastname='Soe', email='[email protected]')
profile.save()
我不斷收到相關聯: 表useradmin_profile沒有名爲usern的列ame_id
任何想法?讚賞。
編輯:
刪除我的數據庫並運行了一個新的執行syncdb,改爲用戶名= models.OneToOneField(用戶)。現在我得到無法分配「u'superuser」「:」Profile.username「必須是」用戶「實例。
恩,謝謝。現在我得到了:不能分配「u'John'」:「Profile.username」必須是一個「用戶」實例。 (John是User.username) – user1059105 2012-03-14 21:06:16
編輯代碼...試試。儘管如此,你將不得不做一個新的syncdb。 – Scalamoosh 2012-03-14 21:18:14
嗯。好吧,嘗試一個新的syncdb。當我嘗試訪問print a.first_name其中a = User時,shell將打印出空白。當我嘗試訪問Profile對象並執行Profile.objects.all()時,我得到[]而不是full_name()。有任何想法嗎? –
user1059105
2012-03-14 21:32:22