django之对django内置的User模型进行自定义扩展方式(django框架核心组件)学会了吗

随心笔谈2年前发布 admin
202 0 0

文章摘要

这篇文章总结了Django User模型中各种方法的功能,重点介绍了以下内容: 1. **用户信息获取**:包括`get_username()`、`get_full_name()`和`get_short_name()`,用于获取用户的基本信息。 2. **密码管理**:`set_password()`用于设置密码,`check_password()`用于验证密码,`set_unusable_password()`用于标记用户无密码。 3. **权限管理**:`group_permissions()`和`get_all_permissions()`用于获取用户通过组赋予的权限,`has_perm()`和`has_perms()`用于检查用户是否具备特定权限。 4. **邮件操作**:`email_user()`用于发送邮件。 文章强调了方法的实用性和功能特点,帮助开发者理解这些方法的实际应用。

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.
Changed in Django 2.1:
In older versions, this also returns False if the password is None or an empty string, or if the password uses a hasher that’s not in the PASSWORD_HASHERS setting. That behavior is considered a bug as it prevents users with such passwords from requesting a password reset.
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 “<app label>.<permission codename>”. (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 “<app label>.<permission codename>”. 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.

© 版权声明

相关文章