gpt4 book ai didi

Django 1.6 AbstractUser无法正常工作

转载 作者:行者123 更新时间:2023-12-04 03:07:09 29 4
gpt4 key购买 nike

尝试创建我的自定义用户模型,这是我的代码:

models.py

class CustomUser(AbstractUser):
USERNAME_FIELD = 'email'

CustomUser._meta.get_field_by_name('email')[0]._unique=True

settings.py
AUTH_USER_MODEL = 'myapp.CustomUser'

进行manage.py syncdb时,发生以下错误:
CommandError: One or more models did not validate:
myapp.customuser: The field named as the USERNAME_FIELD should not be included in REQUIRED_FIELDS on a swappable User model.

谁能给我一些启示?有没有更好的方法来自定义Django 1.6中的用户模型,而无需重写扩展AbstractBaseUser的整个类?

顺便说一句,如果我从代码中删除USERNAME_FIELD ='email'并更改核心django auth/models.py >> AbstractUser定义,则它可以正常工作。我似乎无法覆盖USERNAME_FIELD ...

谢谢!

最佳答案

如错误消息所示,原因是AbstractUser定义了REQUIRED_FIELDS = ['email']。而且您不能在REQUIRED_FIELDS中将一个字段设置为您的USERNAME_FIELD。进一步细节here

因此,如果您希望email作为主要字段,那么前进的方法是扩展并重新定义email而不是AbstractBaseUser上的AbstractUser字段。

如果我没有完全理解您的要求,那么这个question/answer pair也可能与您有关。

关于Django 1.6 AbstractUser无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18853688/

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