【问题标题】:Where is django.contrib.auth.User defined in Django source codeDjango源代码中定义的django.contrib.auth.User在哪里
【发布时间】:2021-06-12 07:03:54
【问题描述】:

Django official guide 中,内容如下:

在这个django.contrib.auth 模型中,有一个User 类,它具有以下属性(用户名、密码、电子邮件、名字、姓氏)。

在github查看源码时,在django.contrib.auth中没有找到这个定义。
我只能在django/contrib/auth/base_user.py on this link 中看到class AbstractBaseUser(models.Model):,在django/contrib/auth/models.py in this webpage 中看到class User(AbstractUser):

Q1:上面官方文档中class models.User是什么意思,意思是Usermodels.py下的一个类?

Q2:如果上面是对的,那么User类在哪里获取用户名、电子邮件等属性?

【问题讨论】:

  • 我在您链接到的文档中找不到inside this ... 部分?

标签: django django-authentication


【解决方案1】:

Q1:上面官方文档中的models.User类是什么意思,也就是说User是models.py下的一个类?

在 Django 中,一个是指带有<i>app_name</i>.<i>ModelName</i> 的模型。所以如果你指定一个模型,这是在<i>app_name</i>/models.py 中实现的,但是由于模型是在models.py 文件中定义的,所以在模型名称中包含它是没有意义的。

例如AUTH_USER_MODEL setting [Django-doc] 的默认值是auth.User,因为应用程序的名称是auth,而模型的名称是User

Q2:如果上面是对的,那么 User 类在哪里获取用户名、电子邮件等属性?

通过继承。事实上,如果我们查看source code of the models.py file [GitHub],我们会看到:

class User(AbstractUser):
    """
    Users within the Django authentication system are represented by this
    model.
    Username and password are required. Other fields are optional.
    """
    class Meta(AbstractUser.Meta):
        swappable = 'AUTH_USER_MODEL'

AbstractUser model [GitHub] 定义了usernameemail 等的字段:

class AbstractUser(AbstractBaseUser, PermissionsMixin):
    # …

    username = models.CharField(
        _('username'),
        max_length=150,
        unique=True,
        help_text=_('Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.'),
        validators=[username_validator],
        error_messages={
            'unique': _("A user with that username already exists."),
        },
    )
    first_name = models.CharField(_('first name'), max_length=150, blank=True)
    last_name = models.CharField(_('last name'), max_length=150, blank=True)
    email = models.EmailField(_('email address'), blank=True)
    is_staff = models.BooleanField(
        _('staff status'),
        default=False,
        help_text=_('Designates whether the user can log into this admin site.'),
    )
    is_active = models.BooleanField(
        _('active'),
        default=True,
        help_text=_(
            'Designates whether this user should be treated as active. '
            'Unselect this instead of deleting accounts.'
        ),
    )
    date_joined = models.DateTimeField(_('date joined'), default=timezone.now)
    
    # …

AbstractUser 是一个抽象模型。这意味着 Django 不会为它创建表。因此,从抽象表继承的模型将继承字段、方法等,然后这些字段将在从 AbstractUser 继承的模型上定义。

【讨论】:

  • 属性date_last_activepassword是在哪里定义的?我没有在AbstractUser 的定义中看到它们。
  • @TianYan:password是在AbstractBaseUser中定义的:github.com/django/django/blob/…因此被AbstractUser继承,而AbstractUserUser继承,所以是“两级继承”。与last_login 相同。
  • class User(AbstractUser): UserID = models.CharField('userID:',help_text='Required', max_length=20, primary_key=True,unique=True, blank=False) NO password 属性在这里定义,因为我认为它会在django.contrib.auth 的某个地方定义。但是,我运行服务器时,弹出错误You are trying to add a non-nullable field 'password' to user without a default,然后我添加password = models.CharField(max_length=20, default="")修复。
  • @TianYan:我认为您最好提出一个新问题,在其中包含相关视图、表单和模型,因为此评论不是很“易于访问”,因为所有项目都写在同一个段落中。
  • 当然可以。编码 5 分钟,故障排除 1 天。这就是我现在的生活.. :) 我想我还有很长的路要走。
猜你喜欢
  • 2022-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-19
相关资源
最近更新 更多