gpt4 book ai didi

python - django.core.exceptions.FieldDoesNotExist : User has no field named 'username'

转载 作者:行者123 更新时间:2023-12-05 01:58:20 26 4
gpt4 key购买 nike

我正在尝试自定义 Django 的 AbstractUser。当我尝试将 username 重置为 None 时,出现以下异常:

"django.core.exceptions.FieldDoesNotExist: User has no field named 'username'".

这是我的代码:

class UserManager(BaseUserManager):
use_in_migrations = True

def _create_user(self, email, password, **extra_fields):
if not email:
raise ValueError("L'adresse e-mail donnée doit etre definie")
email = self.normalize_email(email)
user = self.model(email=email, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user

def create_user(self, email, password, **extra_fields):
extra_fields.setdefault("is_staff", False)
extra_fields.setdefault("is_superuser", False)
return self._create_user(email, password, **extra_fields)

def create_superuser(self, email, password, **extra_fields):
extra_fields.setdefault("is_staff", True)
extra_fields.setdefault("is_superuser", 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)


class User(AbstractUser):
username = None
email = models.EmailField('email adress', unique=True)
telephone = models.CharField(max_length=20)
REQUIRED_FIELDS = []

我该怎么做才能解决这个问题?

最佳答案

如果您在项目中使用 django-allauth,这也可能导致。要解决此问题,您需要在 settings.py 中将 ACCOUNT_USER_MODEL_USERNAME_FIELD 定义为 None。引用这个questionallauth documentation获取更多信息。

ACCOUNT_USER_MODEL_USERNAME_FIELD = None

关于python - django.core.exceptions.FieldDoesNotExist : User has no field named 'username' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68594613/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com