简体   繁体   English

如何扩展django用户模型?

[英]How to extend the django User model?

I'm trying to implement the Users class from django.contrib.auth.models like this: 我正在尝试从django.contrib.auth.models这样实现Users类:

from django.db import models
from django.contrib.auth.models import User


class Registration(models.Model):
    '''Represents a user registration.'''
    user              = models.ForeignKey(User)
    registration_date = models.DateTimeField(auto_now_add=True, help_text='The date of the registration')

    def __str__(self):
        return '%s - %s' % (self.user, self.registration_date,)

This user have two attributes enabled by default: username, password 此用户默认情况下启用了两个属性: 用户名,密码

screenshoot

Reading at the code I can see that there are more attributes, like name and email. 阅读代码,我可以看到还有更多属性,例如名称和电子邮件。

How can I enable those hidden (if this is correct) attributes? 如何启用那些隐藏的 (如果正确的话)属性?

First, these attributes are not hidden. 首先,这些属性没有被隐藏。 Assuming you have "django.contrib.auth" and "django.contrib.contenttypes" in your INSTALLED_APPS , then you have access to the User model as it is defined in your link. 假设您的INSTALLED_APPS具有“ django.contrib.auth”和“ django.contrib.contenttypes”,则您可以访问链接中定义的User模型。 See here for the documentation on how to use/access it. 有关如何使用/访问它的文档,请参见此处

However, since you specified extending the User model, I expect you wanted to add some of your own fields to it (even though your example registration_date exists and is accessible via myuser.date_joined ). 但是,由于您指定扩展 User模型,所以我希望您希望向其中添加一些自己的字段(即使您的示例registration_date存在并且可以通过myuser.date_joined访问)。


The older, more stable and more common way of doing this is similar to what you have. 较旧,更稳定且更常见的方法与您所拥有的类似。 The only difference is to use a OneToOneField(User) instead of a ForeignKey(User) . 唯一的区别是使用OneToOneField(User)而不是ForeignKey(User) This makes the relationship bidirectional and more convenient by forcing one. 通过强制一个关系,可以双向建立关系,并且更加方便。 You do need to make sure and create a Registration object for every User created. 您确实需要确保为每个创建的User创建一个Registration对象。

In fact, there is an example of exactly what you want in the docs for the OneToOneField. 实际上,在OneToOneField文档中确实有一个您想要的示例

from django.db import models
from django.contrib.auth.models import User

class Registration(models.Model):
    user = models.OneToOneField(User)
    registration_date = models.DateTimeField(auto_now_add=True)

>>> user = User.objects.get(pk=1)
>>> registration = Registration.objects.create(user=user)
>>> user.registration.registration_date
# Should give the current time
>>> user.get_full_name()
# Should also have all those *hidden* attributes linked above

As of Django 1.5, you can use your own custom User model fairly easily. 从Django 1.5开始,您可以相当轻松地使用自己的自定义User模型。 The documentation for this feature is here . 此功能的文档在这里 If you are just adding some extra fields, then you can subclass the User model and add your own fields. 如果您只是添加一些额外的字段,则可以对User模型进行子类化并添加自己的字段。

from django.db import models
from django.contrib.auth.models import AbstractUser

class MyUser(AbstractUser):
    # We inherit all those nifty things from AbstractUser
    registration_date = models.DateTimeField(auto_now_add=True)

Then enable it in your settings.py by adding AUTH_USER_MODEL = 'myapp.MyUser' . 然后通过添加AUTH_USER_MODEL = 'myapp.MyUser'在settings.py中启用它。 We now have to access the user model a little differently 现在,我们必须稍微不同地访问用户模型

>>> from django.contrib.auth import get_user_model()
>>> Users = get_user_model()
>>> user = Users.objects.get(pk=1)
>>> user.registration_date
# Should give the current time
>>> user.get_full_name()
# Should have those 'hidden' attributes

All this is available under extending and substituting the User model in the documentation. 扩展替换文档中的“用户”模型下所有这些信息都可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM