何时在Django 1.5中使用自定义用户模型

时间:2022-03-07 19:24:35

I have a question regarding the custom user model in Django 1.5

我对Django 1.5中的自定义用户模型有疑问

So right now the default user model looks just fine to me, I just need to add a few other variables such as gender,location and birthday so that users can fill up those variables after they have successfully registered and activated their account.

所以现在默认的用户模型看起来很好,我只需要添加一些其他变量,如性别,位置和生日,这样用户可以在成功注册并激活他们的帐户后填写这些变量。

So, what is the best way to implement this scenario?

那么,实现这种情况的最佳方法是什么?

Do I have to create a new app called Profile and inherit AbstractBaseUser? and add my custom variable to models.py? Any good example for me to follow?

我是否必须创建一个名为Profile的新应用并继承AbstractBaseUser?并将我的自定义变量添加到models.py?我可以效仿的好榜样吗?

thank you in advance

先谢谢你

1 个解决方案

#1


27  

You want to extend your user model to the AbstractUser and add your additional fields. AbstractUser inherits all of the standard user profile fields, whereas AbstractBaseUser starts you from scratch without any of those fields.

您希望将用户模型扩展到AbstractUser并添加其他字段。 AbstractUser继承了所有标准用户配置文件字段,而AbstractBaseUser从头开始,没有任何这些字段。

It's hard to define best practices this close to the release, but it seems that unless you need to drastically redefine the User model, then you should use AbstractUser where possible.

很难定义接近发布的最佳实践,但似乎除非您需要彻底重新定义User模型,否则您应该尽可能使用AbstractUser。

Here are the docs for extending the User model using AbstractUser

以下是使用AbstractUser扩展User模型的文档

Your models.py would then look something like this:

您的models.py看起来像这样:

class MyUser(AbstractUser):
    gender = models.DateField()
    location = models.CharField()
    birthday = models.CharField()

MyUser will then have the standard email, password, username, etc fields that come with the User model, and your three additional fields above.

然后,MyUser将拥有User模型附带的标准电子邮件,密码,用户名等字段,以及上面的三个附加字段。

Then you need to add the AUTH_USER_MODEL to your settings.py:

然后你需要将AUTH_USER_MODEL添加到settings.py:

AUTH_USER_MODEL = 'myapp.MyUser'

AUTH_USER_MODEL ='myapp.MyUser'

#1


27  

You want to extend your user model to the AbstractUser and add your additional fields. AbstractUser inherits all of the standard user profile fields, whereas AbstractBaseUser starts you from scratch without any of those fields.

您希望将用户模型扩展到AbstractUser并添加其他字段。 AbstractUser继承了所有标准用户配置文件字段,而AbstractBaseUser从头开始,没有任何这些字段。

It's hard to define best practices this close to the release, but it seems that unless you need to drastically redefine the User model, then you should use AbstractUser where possible.

很难定义接近发布的最佳实践,但似乎除非您需要彻底重新定义User模型,否则您应该尽可能使用AbstractUser。

Here are the docs for extending the User model using AbstractUser

以下是使用AbstractUser扩展User模型的文档

Your models.py would then look something like this:

您的models.py看起来像这样:

class MyUser(AbstractUser):
    gender = models.DateField()
    location = models.CharField()
    birthday = models.CharField()

MyUser will then have the standard email, password, username, etc fields that come with the User model, and your three additional fields above.

然后,MyUser将拥有User模型附带的标准电子邮件,密码,用户名等字段,以及上面的三个附加字段。

Then you need to add the AUTH_USER_MODEL to your settings.py:

然后你需要将AUTH_USER_MODEL添加到settings.py:

AUTH_USER_MODEL = 'myapp.MyUser'

AUTH_USER_MODEL ='myapp.MyUser'