Django:是否需要扩展 AbstractBaseUser 才能将电子邮件用作 USERNAME_FIELD?
Django: is extending AbstractBaseUser required to use email as USERNAME_FIELD?
像许多其他人一样,我正在尝试配置我的 Django 应用程序以使用电子邮件作为用户名字段。我有一些已成功迁移到自定义用户模型的现有用户帐户,尽管现在自定义模型与 Django 用户模型相同:
# accounts/models.py
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
pass
每当我尝试将 USERNAME_FIELD
设置为 email
时,我都会收到 USERNAME_FIELD
不能包含在 REQUIRED_FIELDS
中的错误以及它必须是独一无二的。 (旁注:基本用户不需要电子邮件,所以我不明白第一个错误)
有什么方法可以在子类化 AbstractUser 时获得此功能?
或者我是否只需要子类化 AbstractBaseUser
并按照 example 的方式指定每个字段?
如果是后者,我将如何确保定义与 Django 用户模型完全相同的字段?我不想失去我拥有的用户或导致字段不匹配的问题。
为了使用电子邮件地址作为用户名而不得不完全指定自定义模型似乎很愚蠢,所以我可能在这里遗漏了一些东西。如果有一种方法可以确保 Django 用户模型中的唯一字段,我认为这不会成为问题。
正如Django doc所说:
If the changes you need are purely behavioral, and don’t require any change to what is stored in the database, you can create a proxy model based on User.
因为您想用自定义 User
模型替换它:
For instance, on some sites it makes more sense to use an email address as your identification token instead of a username.
您需要通过继承 AbstractBaseUser
来实现您自己的 User
模型。这是一个包含 Django 权限的示例代码:
class User(AbstractBaseUser, PermissionsMixin):
"""
A class implementing a fully featured User model with admin-compliant
permissions.
Email and password are required. Other fields are optional.
"""
email = models.EmailField(
_('Email Address'), unique=True,
error_messages={
'unique': _("A user with that email already exists."),
}
)
username = models.CharField(
_('Username'), max_length=30, unique=True, blank=True, null=True,
help_text=_('30 characters or fewer. Letters, digits and _ only.'),
validators=[
validators.RegexValidator(
r'^\w+$',
_('Enter a valid username. This value may contain only '
'letters, numbers and _ character.'),
'invalid'
),
],
error_messages={
'unique': _("The username is already taken."),
}
)
is_staff = models.BooleanField(
_('Staff Status'), default=False,
help_text=_('Designates whether the user can log into this admin '
'site.')
)
is_active = models.BooleanField(
_('Active'), default=True,
help_text=_('Designates whether this user should be treated as '
'active. Unselect this instead of deleting accounts.')
)
date_joined = models.DateTimeField(_('Date Joined'), default=timezone.now)
objects = UserManager()
USERNAME_FIELD = 'email'
class Meta(object):
verbose_name = _('User')
verbose_name_plural = _('Users')
abstract = False
def get_full_name(self):
"""
Returns email instead of the fullname for the user.
"""
return email_to_name(self.email)
def get_short_name(self):
"""
Returns the short name for the user.
This function works the same as `get_full_name` method.
It's just included for django built-in user comparability.
"""
return self.get_full_name()
def __str__(self):
return self.email
def email_user(self, subject, message, from_email=None, **kwargs):
"""
Sends an email to this User.
"""
send_mail(subject, message, from_email, [self.email], **kwargs)
像许多其他人一样,我正在尝试配置我的 Django 应用程序以使用电子邮件作为用户名字段。我有一些已成功迁移到自定义用户模型的现有用户帐户,尽管现在自定义模型与 Django 用户模型相同:
# accounts/models.py
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
pass
每当我尝试将 USERNAME_FIELD
设置为 email
时,我都会收到 USERNAME_FIELD
不能包含在 REQUIRED_FIELDS
中的错误以及它必须是独一无二的。 (旁注:基本用户不需要电子邮件,所以我不明白第一个错误)
有什么方法可以在子类化 AbstractUser 时获得此功能?
或者我是否只需要子类化 AbstractBaseUser
并按照 example 的方式指定每个字段?
如果是后者,我将如何确保定义与 Django 用户模型完全相同的字段?我不想失去我拥有的用户或导致字段不匹配的问题。
为了使用电子邮件地址作为用户名而不得不完全指定自定义模型似乎很愚蠢,所以我可能在这里遗漏了一些东西。如果有一种方法可以确保 Django 用户模型中的唯一字段,我认为这不会成为问题。
正如Django doc所说:
If the changes you need are purely behavioral, and don’t require any change to what is stored in the database, you can create a proxy model based on User.
因为您想用自定义 User
模型替换它:
For instance, on some sites it makes more sense to use an email address as your identification token instead of a username.
您需要通过继承 AbstractBaseUser
来实现您自己的 User
模型。这是一个包含 Django 权限的示例代码:
class User(AbstractBaseUser, PermissionsMixin):
"""
A class implementing a fully featured User model with admin-compliant
permissions.
Email and password are required. Other fields are optional.
"""
email = models.EmailField(
_('Email Address'), unique=True,
error_messages={
'unique': _("A user with that email already exists."),
}
)
username = models.CharField(
_('Username'), max_length=30, unique=True, blank=True, null=True,
help_text=_('30 characters or fewer. Letters, digits and _ only.'),
validators=[
validators.RegexValidator(
r'^\w+$',
_('Enter a valid username. This value may contain only '
'letters, numbers and _ character.'),
'invalid'
),
],
error_messages={
'unique': _("The username is already taken."),
}
)
is_staff = models.BooleanField(
_('Staff Status'), default=False,
help_text=_('Designates whether the user can log into this admin '
'site.')
)
is_active = models.BooleanField(
_('Active'), default=True,
help_text=_('Designates whether this user should be treated as '
'active. Unselect this instead of deleting accounts.')
)
date_joined = models.DateTimeField(_('Date Joined'), default=timezone.now)
objects = UserManager()
USERNAME_FIELD = 'email'
class Meta(object):
verbose_name = _('User')
verbose_name_plural = _('Users')
abstract = False
def get_full_name(self):
"""
Returns email instead of the fullname for the user.
"""
return email_to_name(self.email)
def get_short_name(self):
"""
Returns the short name for the user.
This function works the same as `get_full_name` method.
It's just included for django built-in user comparability.
"""
return self.get_full_name()
def __str__(self):
return self.email
def email_user(self, subject, message, from_email=None, **kwargs):
"""
Sends an email to this User.
"""
send_mail(subject, message, from_email, [self.email], **kwargs)