何时在Django 1.5中使用自定义用户模型 [英] When to use the Custom User Model in Django 1.5

查看:109
本文介绍了何时在Django 1.5中使用自定义用户模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于Django 1.5中的自定义用户模型的问题。



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



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



我必须创建一个名为配置文件的新应用程序并继承AbstractBaseUser?并将我的自定义变量添加到models.py?任何一个很好的例子,我跟随?



提前谢谢你

解决方案

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

很难定义接近发行版的最佳做法,但似乎除非需要大幅度重新定义User模型,否则您应该使用 AbstractUser



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



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

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

MyUser 然后将具有标准电子邮件,密码,用户名等等用户模型以及您上面的三个其他字段。



然后,您需要将 AUTH_USER_MODEL 添加到您的 settings.py



AUTH_USER_MODEL ='myapp.MyUser' / p>

I have a question regarding the custom user model in 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?

thank you in advance

解决方案

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.

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.

Here are the docs for extending the User model using AbstractUser

Your models.py would then look something like this:

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.

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

AUTH_USER_MODEL = 'myapp.MyUser'

这篇关于何时在Django 1.5中使用自定义用户模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆