Django源码中django.contrib.auth.User定义在哪里

Where is django.contrib.auth.User defined in Django source code

Django official guide中,它是:

Inside this django.contrib.auth model, there is a User class, who has following attributes (username, password, email, first_name, last_name).

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

中看到 class AbstractBaseUser(models.Model):

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

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

Q1: what does class models.User mean in above official document, it means User is a class under models.py?

在 Django 中,一个是指具有 <i>app_name</i>.<i>ModelName</i>[=47= 的模型].因此,如果您指定一个模型,这将在 <code><i>app_name</i>/models.py 中实现,但是由于模型是在models.py 文件,将其包含在模型名称中是没有意义的。

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

Q2: if above is right, then where User class get attributes such as username, email etc?

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

class User(<b>AbstractUser</b>):
    """
    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):
    # …

    <b>username</b> = 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."),
        },
    )
    <b>first_name</b> = models.CharField(_('first name'), max_length=150, blank=True)
    <b>last_name</b> = models.CharField(_('last name'), max_length=150, blank=True)
    <b>email</b> = models.EmailField(_('email address'), blank=True)
    <b>is_staff</b> = models.BooleanField(
        _('staff status'),
        default=False,
        help_text=_('Designates whether the user can log into this admin site.'),
    )
    <b>is_active</b> = models.BooleanField(
        _('active'),
        default=True,
        help_text=_(
            'Designates whether this user should be treated as active. '
            'Unselect this instead of deleting accounts.'
        ),
    )
    <b>date_joined</b> = models.DateTimeField(_('date joined'), default=timezone.now)
    
    # …

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

继承的模型上定义