
User model
- Fields
class models.User
User objects have the following fields:- username
Required. 150 characters or fewer. Usernames may contain alphanumeric, _, @, +, . and - characters.
The max_length should be sufficient for many use cases. If you need a longer length, please use a custom user model. If you use MySQL with the utf8mb4 encoding (recommended for proper Unicode support), specify at most max_length=191 because MySQL can only create unique indexes with 191 characters in that case by default.Usernames and Unicode
Django originally accepted only ASCII letters and numbers in usernames. Although it wasn’t a deliberate choice, Unicode characters have always been accepted when using Python 3. Django 1.10 officially added Unicode support in usernames, keeping the ASCII-only behavior on Python 2, with the option to customize the behavior using User.username_validator.
Changed in Django 1.10:
The max_length increased from 30 to 150 characters. - first_name
Optional (blank=True). 30 characters or fewer.
- last_name
Optional (blank=True). 30 characters or fewer.
- email
Optional (blank=True). Email address.
- password
Required. A hash of, and metadata about, the password. (Django doesn’t store the raw password.) Raw passwords can be arbitrarily long and can contain any character. See the password documentation.
- groups
Many-to-many relationship to Group
- user_permissions
Many-to-many relationship to Permission
- is_staff
Boolean. Designates whether this user can access the admin site.
- is_active
Boolean. Designates whether this user account should be considered active. We recommend that you set this flag to False instead of deleting accounts; that way, if your applications have any foreign keys to users, the foreign keys won’t break.
This doesn’t necessarily control whether or not the user can log in. Authentication backends aren’t required to check for the is_active flag but the default backend (ModelBackend) and the RemoteUserBackend do. You can use AllowAllUsersModelBackend or AllowAllUsersRemoteUserBackend if you want to allow inactive users to login. In this case, you’ll also want to customize the AuthenticationForm used by the LoginView as it rejects inactive users. Be aware that the permission-checking methods such as has_perm() and the authentication in the Django admin all return False for inactive users.
Changed in Django 1.10:
In older versions, ModelBackend and RemoteUserBackend allowed inactive users to authenticate. - is_superuser
Boolean. Designates that this user has all permissions without explicitly assigning them.
- last_login
A datetime of the user’s last login.
- date_joined
A datetime designating when the account was created. Is set to the current date/time by default when the account is created.
- username
- Attributes
class models.User
- is_authenticated
Read-only attribute which is always True (as opposed to AnonymousUser.is_authenticated which is always False). This is a way to tell if the user has been authenticated. This does not imply any permissions and doesn’t check if the user is active or has a valid session. Even though normally you will check this attribute on request.user to find out whether it has been populated by the AuthenticationMiddleware (representing the currently logged-in user), you should know this attribute is True for any User instance.
Changed in Django 1.10:
In older versions, this was a method. Backwards-compatibility support for using it as a method will be removed in Django 2.0.
Don’t use the is operator for comparisons!
To allow the is_authenticated and is_anonymous attributes to also work as methods, the attributes are CallableBool objects. Thus, until the deprecation period ends in Django 2.0, you can’t compare these properties using the is operator. That is, request.user.is_authenticated is True always evaluate to False. - is_anonymous
Read-only attribute which is always False. This is a way of differentiating User and AnonymousUser objects. Generally, you should prefer using is_authenticated to this attribute.
Changed in Django 1.10:
In older versions, this was a method. Backwards-compatibility support for using it as a method will be removed in Django 2.0. - username_validator
New in Django 1.10.
Points to a validator instance used to validate usernames. Defaults to validators.UnicodeUsernameValidator on Python 3 and validators.ASCIIUsernameValidator on Python 2.
To change the default username validator, you can subclass the User model and set this attribute to a different validator instance. For example, to use ASCII usernames on Python 3:from django.contrib.auth.models import User from django.contrib.auth.validators import ASCIIUsernameValidator class CustomUser(User): username_validator = ASCIIUsernameValidator() class Meta: proxy = True # If no new field is added.
- is_authenticated
- Methods
class models.User
- get_username()
Returns the username for the user. Since the User model can be swapped out, you should use this method instead of referencing the username attribute directly.
- get_full_name()
Returns the first_name plus the last_name, with a space in between.
- get_short_name()
Returns the first_name.
- set_password(raw_password)
Sets the user’s password to the given raw string, taking care of the password hashing. Doesn’t save the User object.
When the raw_password is None, the password will be set to an unusable password, as if set_unusable_password() were used. - check_password(raw_password)
Returns True if the given raw string is the correct password for the user. (This takes care of the password hashing in making the comparison.)
- set_unusable_password()
Marks the user as having no password set. This isn’t the same as having a blank string for a password. check_password() for this user will never return True. Doesn’t save the User object.
You may need this if authentication for your application takes place against an existing external source such as an LDAP directory. - has_usable_password()
Returns False if set_unusable_password() has been called for this user.
- get_group_permissions(obj=None)
Returns a set of permission strings that the user has, through their groups.
If obj is passed in, only returns the group permissions for this specific object. - get_all_permissions(obj=None)
Returns a set of permission strings that the user has, both through group and user permissions.
If obj is passed in, only returns the permissions for this specific object.
- has_perm(perm, obj=None)
Returns True if the user has the specified permission, where perm is in the format “
. “. (see documentation on permissions). If the user is inactive, this method will always return False.
If obj is passed in, this method won’t check for a permission for the model, but for this specific object. - has_perms(perm_list, obj=None)
Returns True if the user has each of the specified permissions, where each perm is in the format “
. “. If the user is inactive, this method will always return False.
If obj is passed in, this method won’t check for permissions for the model, but for the specific object. - has_module_perms(package_name)
Returns True if the user has any permissions in the given package (the Django app label). If the user is inactive, this method will always return False.
- email_user(subject, message, from_email=None, **kwargs)
Sends an email to the user. If from_email is None, Django uses the DEFAULT_FROM_EMAIL. Any **kwargs are passed to the underlying send_mail() call.
- get_username()




近期评论