自定义用户模型在`AbstractBaseUser`和`AbstractUser`之间的区别是什么?

huangapple go评论59阅读模式
英文:

What is difference of customization user model between AbstractBaseUser and AbstractUser

问题

我想扩展和自定义内置的Django用户模型。自定义的主要原因是我希望将电子邮件字段作为所有用户的唯一标识,并且我还需要为每个用户添加一些额外的字段。在阅读文档时,我发现了几种实现我的目标的解决方案。其中一种需要继承自AbstractBaseUser

class CustomUser(AbstractBaseUser):
   pass

而另一种解决方案需要继承自AbstractUser

class CustomUser(AbstractUser):
    pass

但我不理解AbstractBaseUserAbstractUser之间的继承差异是什么。

英文:

I want to extend and customize built-in django user model. The main reason of customization is that i want to do email field as unique identifier of all users and also i need to add some extrafields to every user. While reading docs i found several solutions to achieve my goal. One of them require inherite from AbstractBaseUser

class CustomUser(AbstractBaseUser):
   pass

And another solution require to inherite from AbstractUser

class CustomUser(AbstractUser):
    pass

But i didn't understood what is difference of inheritance between AbstractBaseUser and AbstractUser

答案1

得分: 1

Django默认使用username字段进行身份验证。如果您希望使用email地址而不是username作为身份验证方式,那么最好扩展AbstractBaseUser。但是,如果您满意Django如何处理身份验证,但需要在User模型上添加一些额外信息,那么您可以扩展AbstractUser

扩展AbstractBaseUser使您完全控制User模型。

class User(AbstractBaseUser, PermissionsMixin):
     email = models.EmailField(_('email address'), unique=True)
     first_name = models.CharField(_('first name'), max_length=30, blank=True)
     last_name = models.CharField(_('last name'), max_length=30, blank=True)
     date_joined = models.DateTimeField(_('date joined'), auto_now_add=True)
     is_active = models.BooleanField(_('active'), default=True)
     avatar = models.ImageField(upload_to='avatars/', null=True, blank=True)

     objects = UserManager()

     USERNAME_FIELD = 'email'
     REQUIRED_FIELDS = []

扩展AbstractUser使您有机会在Django默认的User模型之上添加额外信息。虽然您可以在扩展的User模型中设置username = NoneUSERNAME_FIELD = 'email',但如果需要对用户模型进行完全控制,选择AbstractBaseUser

class User(AbstractUser):
     father_name = models.CharField(max_length=500, blank=True)
     address = models.CharField(max_length=30, blank=True)
     birth_date = models.DateField()
英文:

Django uses authentication using username field by default. If you want to use an email address as your authentication instead of a username, then it is better to extend AbstractBaseUser. But, if you are happy with how django handle authentication but need some extra information on User model, then you can extend AbstractUser.

Extending AbstractBaseUser gives you full control over User model.

class User(AbstractBaseUser, PermissionsMixin):
     email = models.EmailField(_('email address'), unique=True)
     first_name = models.CharField(_('first name'), max_length=30, blank=True)
     last_name = models.CharField(_('last name'), max_length=30, blank=True)
     date_joined = models.DateTimeField(_('date joined'), auto_now_add=True)
     is_active = models.BooleanField(_('active'), default=True)
     avatar = models.ImageField(upload_to='avatars/', null=True, blank=True)

     objects = UserManager()

     USERNAME_FIELD = 'email'
     REQUIRED_FIELDS = []

Extending AbstractUser gives you an opportunity to add extra information on top of Django's default User model. Although, you can set username = None and USERNAME_FIELD = 'email' at extended User model, if you need full control over User model, choose AbstractBaseUser.

class User(AbstractUser):
     father_name = models.CharField(max_length=500, blank=True)
     address = models.CharField(max_length=30, blank=True)
     birth_date = models.DateField()

huangapple
  • 本文由 发表于 2020年1月4日 02:01:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/59583231.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定