Django User authentication system (1) User object
User object
The User object is the core of the authentication system. User objects are usually used to represent website users and support access control, user registration, and associated creators and content. There is only one user class in the Django Authentication Framework. for example, a super user ('superusers') or ('staff') user only sets different attributes for the same user object.
Default field Fields
Username
Username, required field. It must be 30 or fewer characters and can contain _, @, +,., and-characters.
First_name
Optional. 30 characters or fewer.
Last_name
Optional. 30 characters or fewer.
Email
Email, optional. Email address.
PassWord
Password, required. Django does not store passwords in plain text, but stores hash values.
Groups
User Group. Allow-to-associate relationship to Group
User_permissions
User permissions. Allow-to-allow relationship to Permission
Groups = models. ManyToManyField (Group, verbose_name = _ ('groups '),
Blank = True, help_text = _ ('The groups this user belongs to. A user will'
'Get all permissions granted to each'
'Their groups .'),
Related_name = "user_set", related_query_name = "user ")
User_permissions = models. ManyToManyField (Permission,
Verbose_name = _ ('User permission'), blank = True,
Help_text = _ ('specific permissions for this user .'),
Related_name = "user_set", related_query_name = "user ")
Is_staff
Boolean. Determine whether the user can access the admin management interface. The default value is False.
Is_active
Boolean. Whether the user is active. the default value is True. Generally, the is_active of a user is set to False instead of deleting the user.
Is_superuser
Boolean. The default value is False. If this parameter is set to True, all permissions are granted to the user.
Def has_perm (self, perm, obj = None ):
"""
Returns True if the user has the specified permission. This method
Queries all available auth backends, but returns immediately if any
Backend returns True. Thus, a user who has permission from a single
Auth backend is assumed to have permission in general. If an object is PRovided, permissions for this specific object are checked.
"""
# Active superusers have all permissions.
If self. is_active and self. is_superuser:
Return True
# Otherwise we need to check the backends.
Return _ user_has_perm (self, perm, obj)
Last_login
The last logon time, which is a datetime object. the default value is the current time.
User. last_login = timezone. now ()
Date_joined
Time when the user was created
Method Methods
Is_anonymous ()
Whether it is an anonymous user.
Is_authenticated ()
Whether the user passes the verification and logs in.
Get_full_name ()
Returns first_name plus the last_name, with a space in.
Get_short_name ()
Returns first_name.
Set_password (raw_password)
Set the password.
Check_password (raw_password)
Verify the password.
Get_group_permissions (obj = None)
Returns the set of user group permissions.
Get_all_permissions (obj = None)
Returns all user permissions.
Has_perm (perm, obj = None)
Whether the user has a certain permission. The perm format is ". ".
Has_perms (perm_list, obj = None)
Whether the user has each permission in the permission list.
Create User
Because the password of the User object is not stored in plaintext, the built-in create_user () method is required to create a User object different from the common Model create object.
>>> From django. contrib. auth. models import User
>>> User = User. objects. create_user ('John', 'Lennon @ thebeatles.com ', 'johnpassword ')
# At this point, user is a User object that has already been saved
# To the database. You can continue to change its attributes
# If you want to change other fields.
>>> User. last_name = 'Lennon'
>>> User. save ()
You can also add users in the admin interface.
Create superusers
$ Python manage. py createsuperuser -- username = joe -- email = joe@example.com
Change password
Use the built-in set_password () method.
>>> From django. contrib. auth. models import User
>>> U = User. objects. get (username = 'John ')
>>> U. set_password ('New password ')
>>> U. save ()
Verify user
Authenticate ()
Verify whether the given username and password are valid users. If valid, a User object is returned. if invalid, None is returned.
From django. contrib. auth import authenticate
User = authenticate (username = 'John', password = 'secret ')
If user is not None:
# The password verified for the user
If user. is_active:
Print ("User is valid, active and authenticated ")
Else:
Print ("The password is valid, but the account has been disabled! ")
Else:
# The authentication system was unable to verify the username and password
Print ("The username and password were incorrect .")
The above is the content of the User object in the Django User authentication system (1). For more information, see PHP Chinese network (www.php1.cn )!