Django 我试图更改为自定义用户模型,但出现了错误

Django 我试图更改为自定义用户模型,但出现了错误,django,Django,我试图将Django用户模型更改为自定义用户模型,但出现了一个错误 如何解决错误?谢谢你让我知道~ 步骤1 设置.py AUTH_USER_MODEL = 'accounts.User' 步骤2 accounts/models.py from django.contrib.auth.models import AbstractBaseUser from django.db import models # Create your models here. class User(Abstrac

我试图将Django用户模型更改为自定义用户模型,但出现了一个错误

如何解决错误?谢谢你让我知道~

步骤1 设置.py

AUTH_USER_MODEL = 'accounts.User'

步骤2 accounts/models.py

from django.contrib.auth.models import AbstractBaseUser
from django.db import models

# Create your models here.
class User(AbstractBaseUser):
    email = models.EmailField(blank=True)
    website_url = models.URLField(blank=True)
class User(AbstractBaseUser):
    """User model."""

    username = None
    email = models.EmailField(blank=True)
    website_url = models.URLField(blank=True)


    USERNAME_FIELD = 'email'
步骤3迁移时,出现如下错误

(askcompany) C:\my_django\askcompany>python manage.py makemigrations
错误消息:

  File "C:\Users\hyunsepk\AppData\Local\conda\conda\envs\askcompany\lib\site-packages\django\contrib\auth\checks.py", line 39, in check_user_model
    if cls.USERNAME_FIELD in cls.REQUIRED_FIELDS:
AttributeError: type object 'User' has no attribute 'USERNAME_FIELD'
(askcompany) C:\my_django\askcompany>python manage.py makemigrations
Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\hyunsepk\AppData\Local\conda\conda\envs\askcompany\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "C:\Users\hyunsepk\AppData\Local\conda\conda\envs\askcompany\lib\site-packages\django\core\management\__init__.py", line 377, in execute
    django.setup()
  File "C:\Users\hyunsepk\AppData\Local\conda\conda\envs\askcompany\lib\site-packages\django\__init__.py", line 24, in setup

(askcompany) C:\my_django\askcompany>python manage.py makemigrations
SystemCheckError: System check identified some issues:

ERRORS:
accounts.User: (auth.E003) 'User.email' must be unique 
because it is named as the 'USERNAME_FIELD'.

第四步,我改成了这个,但是发生了另一个错误

accounts/models.py

from django.contrib.auth.models import AbstractBaseUser
from django.db import models

# Create your models here.
class User(AbstractBaseUser):
    email = models.EmailField(blank=True)
    website_url = models.URLField(blank=True)
class User(AbstractBaseUser):
    """User model."""

    username = None
    email = models.EmailField(blank=True)
    website_url = models.URLField(blank=True)


    USERNAME_FIELD = 'email'
错误消息:

  File "C:\Users\hyunsepk\AppData\Local\conda\conda\envs\askcompany\lib\site-packages\django\contrib\auth\checks.py", line 39, in check_user_model
    if cls.USERNAME_FIELD in cls.REQUIRED_FIELDS:
AttributeError: type object 'User' has no attribute 'USERNAME_FIELD'
(askcompany) C:\my_django\askcompany>python manage.py makemigrations
Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\hyunsepk\AppData\Local\conda\conda\envs\askcompany\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "C:\Users\hyunsepk\AppData\Local\conda\conda\envs\askcompany\lib\site-packages\django\core\management\__init__.py", line 377, in execute
    django.setup()
  File "C:\Users\hyunsepk\AppData\Local\conda\conda\envs\askcompany\lib\site-packages\django\__init__.py", line 24, in setup

(askcompany) C:\my_django\askcompany>python manage.py makemigrations
SystemCheckError: System check identified some issues:

ERRORS:
accounts.User: (auth.E003) 'User.email' must be unique 
because it is named as the 'USERNAME_FIELD'.
另一个错误是

ERRORS:
accounts.User: (auth.E002) The field named as the 'USERNAME_FIELD' for a custom user model must not be included in 'REQUIRED_FIELDS'.

您应该继承自
AbstractUser

from django.contrib.auth.models import AbstractUser

class User(AbstractUser):
    """User model."""

    email = models.EmailField(blank=True)
    website_url = models.URLField(blank=True)
如果要将
email
设置为用户名字段,则需要设置
unique=True

class User(AbstractUser):
    """User model."""

    email = models.EmailField(unique=True, blank=True)
    website_url = models.URLField(blank=True)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

您应该继承自
AbstractUser

from django.contrib.auth.models import AbstractUser

class User(AbstractUser):
    """User model."""

    email = models.EmailField(blank=True)
    website_url = models.URLField(blank=True)
如果要将
email
设置为用户名字段,则需要设置
unique=True

class User(AbstractUser):
    """User model."""

    email = models.EmailField(unique=True, blank=True)
    website_url = models.URLField(blank=True)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

错误表明USERNAME\u字段没有属性。 因此,如果您不需要名称字段,您可以告诉Django您将使用电子邮件字段作为用户名字段,如下所示

class User(AbstractBaseUser):
    """User model."""

    username = None
    email = models.EmailField(unique=True,blank=True)
    website_url = models.URLField(blank=True)


    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['username']
查看此文档,这将解释更多内容


错误显示USERNAME\u字段没有属性。 因此,如果您不需要名称字段,您可以告诉Django您将使用电子邮件字段作为用户名字段,如下所示

class User(AbstractBaseUser):
    """User model."""

    username = None
    email = models.EmailField(unique=True,blank=True)
    website_url = models.URLField(blank=True)


    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['username']
查看此文档,这将解释更多内容


因为您的错误明确指出“User.email”必须是唯一的。用户名不能重复或为空。更改您的电子邮件字段,如下所示-

email = models.EmailField(unique=True)
class User(AbstractBaseUser):
    username = models.CharField(db_index=True, unique=True, max_length=255)
    email = models.EmailField(unique=True)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['username']
它应该会起作用

编辑- 更改您的用户模型,如下所示-

email = models.EmailField(unique=True)
class User(AbstractBaseUser):
    username = models.CharField(db_index=True, unique=True, max_length=255)
    email = models.EmailField(unique=True)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['username']

如果您想使用权限,我建议您也将PermissionsMixin继承给您的用户,或者继承AbstractUser而不是AbstractBaseUser。

因为您的错误明确指出“user.email”必须是唯一的。用户名不能重复或为空。更改您的电子邮件字段,如下所示-

email = models.EmailField(unique=True)
class User(AbstractBaseUser):
    username = models.CharField(db_index=True, unique=True, max_length=255)
    email = models.EmailField(unique=True)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['username']
它应该会起作用

编辑- 更改您的用户模型,如下所示-

email = models.EmailField(unique=True)
class User(AbstractBaseUser):
    username = models.CharField(db_index=True, unique=True, max_length=255)
    email = models.EmailField(unique=True)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['username']

如果您想使用权限,我建议您也将PermissionsMixin继承给您的用户,或者继承AbstractUser而不是AbstractBaseUser。

当我这样做时,我遇到了另一个错误,因此我更新了question@hyunk那是什么错误?这可能是因为之前存在的migrations@hyunk如果您正在谈论编辑中的错误,请参阅我的更新以了解解决方案。清除所有迁移文件是否可以解决问题?当我这样做时,我遇到了另一个错误,因此我更新了question@hyunk那是什么错误?这可能是因为之前存在的migrations@hyunk如果您正在谈论编辑中的错误,请参阅我的更新以了解解决方案。清除所有迁移文件是否可以解决问题?当我这样做时,我遇到了另一个错误,因此我在这样做时更新了问题,我遇到了另一个错误,所以我更新了问题我改成了这个,但是出现了另一个错误=>accounts.User:(auth.E002)自定义用户模型中名为“USERNAME\u field”的字段不能包含在“REQUIRED\u FIELDS”中。很好,这是正确的答案,谢谢Arvind KumarI对此进行了更改,但是出现了另一个错误=>accounts。用户:(auth.E002)自定义用户模型中名为“用户名\字段”的字段不能包含在“必填字段”中。很好,这是工作,回答准确,谢谢Arvind Kumardos这回答了你的问题吗?这回答了你的问题吗?