当前位置:   article > 正文

Django(60)Django内置User模型源码分析及自定义User

class user(abstractuser):

前言

Django为我们提供了内置的User模型,不需要我们再额外定义用户模型,建立用户体系了。它的完整的路径是在django.contrib.auth.models.User

User模型源码分析

  1. class User(AbstractUser):
  2. """
  3. Django 身份验证系统中的用户由该模型表示
  4. 需要用户名和密码。其他字段是可选的。
  5. """
  6. class Meta(AbstractUser.Meta):
  7. swappable = 'AUTH_USER_MODEL'

我们可以看到User这个类本身没干什么事情,而是继承自AbstractUser类,那么我们查看下AbstractUser的源码

  1. class AbstractUser(AbstractBaseUser, PermissionsMixin):
  2. """
  3. 一个抽象基类实现了一个功能齐全的用户模型 符合管理员的权限。
  4. 需要用户名和密码。 其他字段是可选的。
  5. """
  6. # 用户民校验
  7. username_validator = UnicodeUsernameValidator()
  8. username = models.CharField(
  9. _('username'),
  10. max_length=150,
  11. unique=True,
  12. help_text=_('Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.'),
  13. validators=[username_validator],
  14. error_messages={
  15. 'unique': _("A user with that username already exists."),
  16. },
  17. )
  18. first_name = models.CharField(_('first name'), max_length=30, blank=True)
  19. last_name = models.CharField(_('last name'), max_length=150, blank=True)
  20. email = models.EmailField(_('email address'), blank=True)
  21. is_staff = models.BooleanField(
  22. _('staff status'),
  23. default=False,
  24. help_text=_('Designates whether the user can log into this admin site.'),
  25. )
  26. is_active = models.BooleanField(
  27. _('active'),
  28. default=True,
  29. help_text=_(
  30. 'Designates whether this user should be treated as active. '
  31. 'Unselect this instead of deleting accounts.'
  32. ),
  33. )
  34. date_joined = models.DateTimeField(_('date joined'), default=timezone.now)
  35. # objects用户管理,里面有创建用户的方法
  36. objects = UserManager()
  37. EMAIL_FIELD = 'email'
  38. # 用来描述User模型名字字段的字符串,作为唯一的标识。如果没有修改,那么会使用USERNAME来作为唯一字段。
  39. USERNAME_FIELD = 'username'
  40. # 一个字段名列表,用于当通过createsuperuser管理命令创建一个用户时的提示。
  41. REQUIRED_FIELDS = ['email']
  42. class Meta:
  43. verbose_name = _('use
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/306461
推荐阅读
相关标签
  

闽ICP备14008679号