2016-05-31 100 views
-1

建議爲用戶創建一個新的model.py自己的登錄和管理?或者更喜歡將字段添加到提供Django的用戶模型?Django中的用戶管理

我希望有一個用戶列表,其中包括身份證號碼,出生日期,電子郵件,持有的職位等等。但我不確定是否修改提供Django的用戶或創建新模型。

你有什麼建議?

+0

的可能的複製[Django的 - 用戶表單額外的字段(http://stackoverflow.com/questions/24945440/django-extra-fields-for-user-form) –

+0

你應該檢查出的https: //docs.djangoproject.com/en/1.9/topics/auth/customizing/#extending-the-existing-user-model – slider

回答

1

我認爲最好創建一個Profile模型,其中用戶字段字段與User實例具有OneToOneField相關性。

from django.conf import settings 
. 
. 

class Profile(models.Model): 
    user=models.OneToOneField(settings.AUTH_USER_MODEL)#creates onetoone relationship with the user model 
    IdNo=models.PositiveIntegerField() 
    email=models.EmailField() 
    date_of_birth=models.DateField("DOB",help_text="YYYY-MM-DD",blank=True,null=True) 
    def __unicode__(self): 
     return '{} Profile'.format(self.user.username) 
0

Django的身份驗證軟件包已經很好的定義和強大。我強烈建議你爲用戶使用原始的Django模型。

但是,對於您的問題,要在用戶模型中添加額外的字段,可以通過將用戶引用到具有外鍵的用戶模型來自定義您自己的模型。