Yan Tian
Yan Tian

Reputation: 397

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

In Django official guide, it reads:

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

When I check the source code in github, I did not find this definition in django.contrib.auth.
I can only see class AbstractBaseUser(models.Model): in django/contrib/auth/base_user.py on this link, and class User(AbstractUser): in django/contrib/auth/models.py in this webpage.

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

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

Upvotes: 2

Views: 686

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477160

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

In Django one refers to a model with the app_name.ModelName. So if you specify a model, this is implemented in the app_name/models.py, but since models are defined in the models.py file, it makes no sense to include that in the name of the model.

For example the default for the AUTH_USER_MODEL setting [Django-doc] is auth.User, since the name of the app is auth, and the name of the model is User.

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

Through inheritance. Indeed if we look at the source code of the models.py file [GitHub], we see:

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'

and the AbstractUser model [GitHub] defines the fields for username, email, etc.:

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 is an abstract model. This means that Django does not create a table for it. Models that inherit from an abstract table will thus inherit fields, methods, etc. and these fields will then be defined on the model that inherits from AbstractUser.

Upvotes: 3

Related Questions