Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何扩展django用户模型?_Python_Django_Model - Fatal编程技术网

Python 如何扩展django用户模型?

Python 如何扩展django用户模型?,python,django,model,Python,Django,Model,我试图从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) re

我试图从
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,)
此用户在默认情况下启用了两个属性:用户名、密码

在阅读时,我可以看到更多的属性,如姓名和电子邮件


如何启用这些隐藏(如果这是正确的)属性

首先,这些属性不会隐藏。假设您在
已安装的应用程序中有“django.contrib.auth”和“django.contrib.contenttypes”,则您可以访问链接中定义的
用户
模型。有关如何使用/访问它的文档,请参阅

但是,由于您指定扩展
用户
模型,我希望您希望向其中添加一些自己的字段(即使您的示例
注册日期
存在,并且可以通过
myuser.date
访问)


更古老、更稳定、更常见的方法与您的方法类似。唯一的区别是使用
OneToOneField(用户)
而不是
ForeignKey(用户)
。这通过强制一个,使关系双向且更方便。您确实需要确保并为每个创建的
用户创建一个
注册
对象

事实上,在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

从Django 1.5开始,您可以相当轻松地使用自己的自定义用户模型。此功能的文档如下所示。如果您只是添加一些额外的字段,那么您可以对用户模型进行子类化并添加自己的字段

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)
然后通过添加
AUTH\u USER\u MODEL='myapp.MyUser'
在settings.py中启用它。我们现在必须以稍微不同的方式访问用户模型

>>> 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


所有这些都可以在文档中的用户模型下找到。

首先,这些属性不会隐藏。假设您在
已安装的应用程序中有“django.contrib.auth”和“django.contrib.contenttypes”,则您可以访问链接中定义的
用户
模型。有关如何使用/访问它的文档,请参阅

但是,由于您指定扩展
用户
模型,我希望您希望向其中添加一些自己的字段(即使您的示例
注册日期
存在,并且可以通过
myuser.date
访问)


更古老、更稳定、更常见的方法与您的方法类似。唯一的区别是使用
OneToOneField(用户)
而不是
ForeignKey(用户)
。这通过强制一个,使关系双向且更方便。您确实需要确保并为每个创建的
用户创建一个
注册
对象

事实上,在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

从Django 1.5开始,您可以相当轻松地使用自己的自定义用户模型。此功能的文档如下所示。如果您只是添加一些额外的字段,那么您可以对用户模型进行子类化并添加自己的字段

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)
然后通过添加
AUTH\u USER\u MODEL='myapp.MyUser'
在settings.py中启用它。我们现在必须以稍微不同的方式访问用户模型

>>> 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


所有这些都可以在文档中的“添加新用户”和“用户模型”下获得。

但是,我可以从“添加新用户”表单中输入哪些属性?例如,你可以在我的截图中看到,我只能输入用户名和密码,但我想使用姓名、电子邮件和其他信息。这些属性在默认模型上,所以我不想覆盖它们。@我明白了,你的问题是关于扩展django管理表单的。看看底部的完整示例,特别是其中的“应用程序的admin.py文件中需要以下代码”。当我有时间测试我的帖子时,我会更新它。但是谁可以从添加新用户表单中输入这些属性呢?例如,你可以在我的截图中看到,我只能输入用户名和密码,但我想使用姓名、电子邮件和其他信息。这些属性在默认模型上,所以我不想覆盖它们。@我明白了,你的问题是关于扩展django管理表单的。看看底部的完整示例,特别是其中的“应用程序的admin.py文件中需要以下代码”。我会在有时间测试后更新我的帖子。