Django 1.5+ allows us to add custom fields to a User. I want to use this fact, but I don't necessarily know what is good practice. Here is a situation I am confused on how to handle the models.
Django 1.5+允许我们向用户添加自定义字段。我想用这个事实,但我不一定知道什么是好的做法。这是一个我对如何处理模型感到困惑的情况。
Given the option to add fields to User
, if a project only has one type of User
, lets say a Student
model, can I simply add student-specific fields to User
? I am new to Django, but I believe the alternative would be to set up general User
settings, and create a Student
model, and a one-to-one unique field in it call user.
给定向User添加字段的选项,如果项目只有一种类型的用户,让我们说一个学生模型,我可以简单地将学生特定的字段添加到用户吗?我是Django的新手,但我相信另一种方法是设置一般的用户设置,并创建一个学生模型,并在其中调用一对一的唯一字段。
Should you ever expand a Django User
's fields to mimic that of a model, even if the project is guaranteed only to have one type of user?
您是否应该扩展Django用户的字段来模仿模型的字段,即使项目只保证只有一种类型的用户?
2 个解决方案
#1
12
If you only have one type of user and are using Django 1.5+, I would recommend taking advantage of the new AbstractUser. Extending Django's default user
如果您只有一种类型的用户并使用Django 1.5+,我建议您利用新的AbstractUser。扩展Django的默认用户
As an example where you want to add date of birth and favorite color:
作为您想要添加出生日期和喜欢的颜色的示例:
#myusers/models.py
from django.contrib.auth.models import AbstractUser
from django.db import models
class MyUser(AbstractUser):
dob = models.DateField()
favorite_color = models.CharField(max_length=32, default='Blue')
If you need more flexibility you can extend the AbstractBaseUser instead of AbstractUser, but for most basic cases you should only need AbstractUser.
如果您需要更多灵活性,可以扩展AbstractBaseUser而不是AbstractUser,但对于大多数基本情况,您应该只需要AbstractUser。
Also note that in either case, you'll need to reference your user model by using settings.AUTH_USER_MODEL.
另请注意,在任何一种情况下,您都需要使用settings.AUTH_USER_MODEL来引用您的用户模型。
Using out example above and assuming the app it was defined in is called myusers
:
使用上面的示例并假设其中定义的应用程序称为myusers:
#settings.py
AUTH_USER_MODEL = 'myusers.MyUser'
The method you mention of creating a Student model with a one-to-one field to the User model still works, but is not as clean (there are still cases where it makes sense if you have multiple kinds of users).
您提到的为User模型创建具有一对一字段的Student模型的方法仍然有效,但不是很干净(如果您有多种用户,仍然存在有意义的情况)。
I don't normally like to reference books in answers, but I found that Two Scoops of Django's, Chapter 16 on the User model gave a much clearer explanation of where the different options are appropriate than the current version of the online Django docs. The book is overall a very useful intro to Django and was written based on 1.5. You'd have to buy the book or find someone who has it, though... (FYI: I don't get any money recommending this).
我通常不喜欢在答案中引用书籍,但我发现用户模型的第16章Django的两个Scoops更清楚地解释了不同选项适用于当前版本的在线Django文档的位置。这本书对Django来说是一个非常有用的介绍,并且是基于1.5编写的。你必须买这本书或者找一个拥有它的人,不过......(仅供参考:我没有任何资金推荐这个)。
You could also take a look at this SO question/answer: https://*.com/a/14104748/307293
你也可以看一下这个问题/答案:https://*.com/a/14104748/307293
#2
0
You shouldn't touch the django contributed User
model (from the authentication framework). This will break upgrades and you do not know what other consequences it might have.
您不应该触摸django贡献的用户模型(来自身份验证框架)。这将打破升级,你不知道它可能带来的其他后果。
There are two basic ways to do this:
有两种基本方法可以做到这一点:
-
If you just need to store additional information about a user, but don't need to change how the authentication/authorization mechanism works, create a model and add a
OneToOneField
to theUser
model. In this model, store any other miscellaneous information.如果您只需要存储有关用户的其他信息,但不需要更改身份验证/授权机制的工作方式,请创建模型并将OneToOneField添加到User模型。在此模型中,存储任何其他杂项信息。
-
If you want to change how authentication works you can create your own
User
model and have django use that (1.5+ only).如果您想要更改身份验证的工作方式,您可以创建自己的用户模型并让django使用它(仅限1.5+)。
#1
12
If you only have one type of user and are using Django 1.5+, I would recommend taking advantage of the new AbstractUser. Extending Django's default user
如果您只有一种类型的用户并使用Django 1.5+,我建议您利用新的AbstractUser。扩展Django的默认用户
As an example where you want to add date of birth and favorite color:
作为您想要添加出生日期和喜欢的颜色的示例:
#myusers/models.py
from django.contrib.auth.models import AbstractUser
from django.db import models
class MyUser(AbstractUser):
dob = models.DateField()
favorite_color = models.CharField(max_length=32, default='Blue')
If you need more flexibility you can extend the AbstractBaseUser instead of AbstractUser, but for most basic cases you should only need AbstractUser.
如果您需要更多灵活性,可以扩展AbstractBaseUser而不是AbstractUser,但对于大多数基本情况,您应该只需要AbstractUser。
Also note that in either case, you'll need to reference your user model by using settings.AUTH_USER_MODEL.
另请注意,在任何一种情况下,您都需要使用settings.AUTH_USER_MODEL来引用您的用户模型。
Using out example above and assuming the app it was defined in is called myusers
:
使用上面的示例并假设其中定义的应用程序称为myusers:
#settings.py
AUTH_USER_MODEL = 'myusers.MyUser'
The method you mention of creating a Student model with a one-to-one field to the User model still works, but is not as clean (there are still cases where it makes sense if you have multiple kinds of users).
您提到的为User模型创建具有一对一字段的Student模型的方法仍然有效,但不是很干净(如果您有多种用户,仍然存在有意义的情况)。
I don't normally like to reference books in answers, but I found that Two Scoops of Django's, Chapter 16 on the User model gave a much clearer explanation of where the different options are appropriate than the current version of the online Django docs. The book is overall a very useful intro to Django and was written based on 1.5. You'd have to buy the book or find someone who has it, though... (FYI: I don't get any money recommending this).
我通常不喜欢在答案中引用书籍,但我发现用户模型的第16章Django的两个Scoops更清楚地解释了不同选项适用于当前版本的在线Django文档的位置。这本书对Django来说是一个非常有用的介绍,并且是基于1.5编写的。你必须买这本书或者找一个拥有它的人,不过......(仅供参考:我没有任何资金推荐这个)。
You could also take a look at this SO question/answer: https://*.com/a/14104748/307293
你也可以看一下这个问题/答案:https://*.com/a/14104748/307293
#2
0
You shouldn't touch the django contributed User
model (from the authentication framework). This will break upgrades and you do not know what other consequences it might have.
您不应该触摸django贡献的用户模型(来自身份验证框架)。这将打破升级,你不知道它可能带来的其他后果。
There are two basic ways to do this:
有两种基本方法可以做到这一点:
-
If you just need to store additional information about a user, but don't need to change how the authentication/authorization mechanism works, create a model and add a
OneToOneField
to theUser
model. In this model, store any other miscellaneous information.如果您只需要存储有关用户的其他信息,但不需要更改身份验证/授权机制的工作方式,请创建模型并将OneToOneField添加到User模型。在此模型中,存储任何其他杂项信息。
-
If you want to change how authentication works you can create your own
User
model and have django use that (1.5+ only).如果您想要更改身份验证的工作方式,您可以创建自己的用户模型并让django使用它(仅限1.5+)。