用自定义字段扩展User模型(与Django的身份验证应用程序绑定)的最佳方法是什么?我还可能想使用电子邮件作为用户名(用于身份验证)。
我已经看到了一些方法,但不能决定哪一个是最好的。
用自定义字段扩展User模型(与Django的身份验证应用程序绑定)的最佳方法是什么?我还可能想使用电子邮件作为用户名(用于身份验证)。
我已经看到了一些方法,但不能决定哪一个是最好的。
当前回答
注意:这个答案是不赞成的。如果你使用的是Django 1.7或更高版本,请参阅其他答案。
我就是这么做的。
#in models.py
from django.contrib.auth.models import User
from django.db.models.signals import post_save
class UserProfile(models.Model):
user = models.OneToOneField(User)
#other fields here
def __str__(self):
return "%s's profile" % self.user
def create_user_profile(sender, instance, created, **kwargs):
if created:
profile, created = UserProfile.objects.get_or_create(user=instance)
post_save.connect(create_user_profile, sender=User)
#in settings.py
AUTH_PROFILE_MODULE = 'YOURAPP.UserProfile'
这将在每次保存用户时创建一个用户配置文件。 然后你可以使用
user.get_profile().whatever
以下是文档中的更多信息
http://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users
更新:请注意AUTH_PROFILE_MODULE自v1.5起已弃用:https://docs.djangoproject.com/en/1.5/ref/settings/#auth-profile-module
其他回答
简单而有效的方法是 models.py
from django.contrib.auth.models import User
class CustomUser(User):
profile_pic = models.ImageField(upload_to='...')
other_field = models.CharField()
注意:这个答案是不赞成的。如果你使用的是Django 1.7或更高版本,请参阅其他答案。
我就是这么做的。
#in models.py
from django.contrib.auth.models import User
from django.db.models.signals import post_save
class UserProfile(models.Model):
user = models.OneToOneField(User)
#other fields here
def __str__(self):
return "%s's profile" % self.user
def create_user_profile(sender, instance, created, **kwargs):
if created:
profile, created = UserProfile.objects.get_or_create(user=instance)
post_save.connect(create_user_profile, sender=User)
#in settings.py
AUTH_PROFILE_MODULE = 'YOURAPP.UserProfile'
这将在每次保存用户时创建一个用户配置文件。 然后你可以使用
user.get_profile().whatever
以下是文档中的更多信息
http://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users
更新:请注意AUTH_PROFILE_MODULE自v1.5起已弃用:https://docs.djangoproject.com/en/1.5/ref/settings/#auth-profile-module
下面是另一种扩展User的方法。 我觉得它比上面两种方法更清晰,简单,易读。
http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/
使用上述方法:
你不需要使用 user.get_profile()。Newattribute访问额外的信息 与用户相关 你可以直接访问 通过 user.newattribute
最简单也是django推荐的方法是通过一个OneToOneField(User)属性。
扩展现有的User模型 … 如果您希望存储与User相关的信息,您可以使用与包含附加信息字段的模型的一对一关系。这种一对一的模型通常称为概要文件模型,因为它可能存储有关站点用户的非身份验证相关信息。
也就是说,扩展django.contrib.auth.models.User并替换它也可以…
替换自定义用户模型 有些类型的项目可能有身份验证要求,而Django内置的User模型并不总是合适的。例如,在一些网站上,使用电子邮件地址代替用户名更有意义。 [Ed:两个警告和一个通知,提到这是相当激烈的。]
我绝对不会去修改Django源代码树中的User类,也不会复制和修改认证模块。
从Django 1.5开始,你可以很容易地扩展用户模型,在数据库中只保留一个表。
from django.contrib.auth.models import AbstractUser
from django.db import models
from django.utils.translation import ugettext_lazy as _
class UserProfile(AbstractUser):
age = models.PositiveIntegerField(_("age"))
您还必须在设置文件中将其配置为当前用户类
# supposing you put it in apps/profiles/models.py
AUTH_USER_MODEL = "profiles.UserProfile"
如果你想添加很多用户的首选项,OneToOneField选项可能是一个更好的选择。
给开发第三方库的人一个提示:如果您需要访问用户类,请记住人们可以更改它。使用官方帮助获取正确的类
from django.contrib.auth import get_user_model
User = get_user_model()