英文:
What is difference of customization user model between AbstractBaseUser and AbstractUser
问题
我想扩展和自定义内置的Django用户模型。自定义的主要原因是我希望将电子邮件字段作为所有用户的唯一标识,并且我还需要为每个用户添加一些额外的字段。在阅读文档时,我发现了几种实现我的目标的解决方案。其中一种需要继承自AbstractBaseUser
:
class CustomUser(AbstractBaseUser):
pass
而另一种解决方案需要继承自AbstractUser
:
class CustomUser(AbstractUser):
pass
但我不理解AbstractBaseUser
和AbstractUser
之间的继承差异是什么。
英文:
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 = None
和USERNAME_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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论