为什么 Django 管理员登录页面抛出 FieldError?
why the Django admin login page throwing FieldError?
奇怪的事情发生了。我已经创建了自定义用户模型。直到今天它都正常工作。我不知道出了什么问题。当我启动管理页面登录时,我在单击 'login' 后看到以下错误。我使用 'email' 作为用户名
FieldError at /admin/login/
Cannot resolve keyword 'username' into field. Choices are: auth_token, auth_token_set, contact, dob, email, employee_code, first_name, groups, id, is_active, is_staff, is_superuser, last_login, last_name, logentry, password, user_permissions
- 我试图删除迁移文件并重新运行迁移,但没有用。
- 然后我完全删除了应用程序并从头开始配置所有内容。还是一样的错误。
Here 是完整的错误堆栈
models.py
from django.db import models
from django.contrib.auth.base_user import BaseUserManager
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, AbstractUser
from django.utils.translation import gettext_lazy as _
class AccountManager(BaseUserManager):
def create_superuser(self, email, password, **extra_fields):
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
extra_fields.setdefault('is_active', True)
if extra_fields.get('is_staff') is not True:
raise ValueError(_('Superuser must have is_staff=True.'))
if extra_fields.get('is_superuser') is not True:
raise ValueError(_('Superuser must have is_superuser=True.'))
return self.create_user(email, password, **extra_fields)
def create_user(self, email, password, **extra_fields):
extra_fields.setdefault('is_staff', False)
extra_fields.setdefault('is_superuser', False)
if not email:
raise ValueError(_('Enter the email before proceeding'))
email = self.normalize_email(email)
user = self.model(email=email, password=password, **extra_fields)
user.set_password(password)
user.save()
return user
class NewEmployeeProfile(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(unique=True)
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
employee_code = models.CharField(max_length=10, null=True)
contact = models.CharField(max_length=10, null=True)
dob = models.DateField(blank=True, null=True)
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.'
),
)
objects = AccountManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['first_name', 'last_name', 'contact']
def __str__(self):
return str(self.email)
查看您的回溯,您似乎正在使用包 drf_registration
并使用 drf_registration.auth.MultiFieldsModelBackend
作为身份验证后端。默认情况下,此包使用列表 ['username', 'email',]
(参见 Settings (drf_registration - GitHub))作为可能的字段,这些字段将作为用户名提供,以过滤执行身份验证的用户模型。
您的自定义用户模型没有 username
字段,因此这会给您一个错误。
要更改此后端使用的字段,您需要设置设置 LOGIN_USERNAME_FIELDS
,如下 settings.py
:
LOGIN_USERNAME_FIELDS = ['email',]
奇怪的事情发生了。我已经创建了自定义用户模型。直到今天它都正常工作。我不知道出了什么问题。当我启动管理页面登录时,我在单击 'login' 后看到以下错误。我使用 'email' 作为用户名
FieldError at /admin/login/
Cannot resolve keyword 'username' into field. Choices are: auth_token, auth_token_set, contact, dob, email, employee_code, first_name, groups, id, is_active, is_staff, is_superuser, last_login, last_name, logentry, password, user_permissions
- 我试图删除迁移文件并重新运行迁移,但没有用。
- 然后我完全删除了应用程序并从头开始配置所有内容。还是一样的错误。
Here 是完整的错误堆栈
models.py
from django.db import models
from django.contrib.auth.base_user import BaseUserManager
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, AbstractUser
from django.utils.translation import gettext_lazy as _
class AccountManager(BaseUserManager):
def create_superuser(self, email, password, **extra_fields):
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
extra_fields.setdefault('is_active', True)
if extra_fields.get('is_staff') is not True:
raise ValueError(_('Superuser must have is_staff=True.'))
if extra_fields.get('is_superuser') is not True:
raise ValueError(_('Superuser must have is_superuser=True.'))
return self.create_user(email, password, **extra_fields)
def create_user(self, email, password, **extra_fields):
extra_fields.setdefault('is_staff', False)
extra_fields.setdefault('is_superuser', False)
if not email:
raise ValueError(_('Enter the email before proceeding'))
email = self.normalize_email(email)
user = self.model(email=email, password=password, **extra_fields)
user.set_password(password)
user.save()
return user
class NewEmployeeProfile(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(unique=True)
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
employee_code = models.CharField(max_length=10, null=True)
contact = models.CharField(max_length=10, null=True)
dob = models.DateField(blank=True, null=True)
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.'
),
)
objects = AccountManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['first_name', 'last_name', 'contact']
def __str__(self):
return str(self.email)
查看您的回溯,您似乎正在使用包 drf_registration
并使用 drf_registration.auth.MultiFieldsModelBackend
作为身份验证后端。默认情况下,此包使用列表 ['username', 'email',]
(参见 Settings (drf_registration - GitHub))作为可能的字段,这些字段将作为用户名提供,以过滤执行身份验证的用户模型。
您的自定义用户模型没有 username
字段,因此这会给您一个错误。
要更改此后端使用的字段,您需要设置设置 LOGIN_USERNAME_FIELDS
,如下 settings.py
:
LOGIN_USERNAME_FIELDS = ['email',]