Django继承AbstractUser和AbstractBaseUser时迁移问题
(DRF) ale@ubuntu:~/project/Aley$ python manage.py makemigrations users
Traceback (most recent call last):
File "manage.py", line 22, in <module>
main()
File "manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/home/ale/Envs/DRF/lib/python3.6/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/home/ale/Envs/DRF/lib/python3.6/site-packages/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/ale/Envs/DRF/lib/python3.6/site-packages/django/core/management/base.py", line 330, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/ale/Envs/DRF/lib/python3.6/site-packages/django/core/management/base.py", line 368, in execute
self.check()
File "/home/ale/Envs/DRF/lib/python3.6/site-packages/django/core/management/base.py", line 396, in check
databases=databases,
File "/home/ale/Envs/DRF/lib/python3.6/site-packages/django/core/checks/registry.py", line 70, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "/home/ale/Envs/DRF/lib/python3.6/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"
当Django迁移模型时报304错误,解决方法:
当你的继承AbstractUser类时,会使用django的用户认证,需要在项目的setting.py 中加入AUTH_USER_MODEL=”app.model”
# 用户认证
AUTH_USER_MODEL = "users.User"
——————————————————————————————————————————————————
(DRF) ale@ubuntu:~/project/Aley$ python manage.py makemigrations users
Traceback (most recent call last):
File "manage.py", line 22, in <module>
main()
File "manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/home/ale/Envs/DRF/lib/python3.6/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/home/ale/Envs/DRF/lib/python3.6/site-packages/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/ale/Envs/DRF/lib/python3.6/site-packages/django/core/management/base.py", line 330, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/ale/Envs/DRF/lib/python3.6/site-packages/django/core/management/base.py", line 368, in execute
self.check()
File "/home/ale/Envs/DRF/lib/python3.6/site-packages/django/core/management/base.py", line 396, in check
databases=databases,
File "/home/ale/Envs/DRF/lib/python3.6/site-packages/django/core/checks/registry.py", line 70, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "/home/ale/Envs/DRF/lib/python3.6/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"
迁移模型时报 has no attribute “USERNAME_FIELD”错误时时,是因为模型继承自AbstractBaseUser,只需在模型中添加 USERNAME_FIELD= “username”
class User(BaseModel, AbstractBaseUser):
username = models.CharField(max_length=128, unique=True, verbose_name="用户名")
phone = models.IntegerField(verbose_name="手机号")
email = models.CharField(max_length=128, verbose_name="邮箱")
USERNAME_FIELD = "username" # 加上这句代码
def __str__(self):
return self.username
class Meta:
db_table = "aley_user"
verbose_name = "用户"
verbose_name_plural = verbose_name