Django:与创建自定义用户有关的问题

问题描述 投票:-1回答:1

我正在尝试使用AbstractUser类在Django中创建自定义用户。

在其中定义我的custom_user的models.py代码。

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

class MyUser(AbstractUser):
    country=models.CharField(max_length=20)

我只希望国家作为用户的额外字段。这就是为什么我只定义了国家/地区。

我已经在settings.py中提到了我的自定义用户模型。

AUTH_USER_MODEL = 'new_user.MyUser'

[当我运行迁移命令时,它运行成功,但是当我运行迁移时,它给我error.like这样。

Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\Futuresoft\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "C:\Users\Futuresoft\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\Futuresoft\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\Futuresoft\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "C:\Users\Futuresoft\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\base.py", line 83, in
wrapped
    res = handle_func(*args, **kwargs)
  File "C:\Users\Futuresoft\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\commands\migrate.py",
line 90, in handle
    executor.loader.check_consistent_history(connection)
  File "C:\Users\Futuresoft\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\migrations\loader.py", line 299, in check_consistent_history
    connection.alias,
django.db.migrations.exceptions.InconsistentMigrationHistory: Migration admin.0001_initial is applied before its dependency new_user.0001_initial on database 'default'.

我不知道这是什么。任何人都可以提供帮助。

注意:我正在尝试在创建SuperUser之后创建自定义用户,并且我已经迁移了我们第一次创建新项目时出现的所有迁移。

python django django-migrations django-custom-user
1个回答
2
投票

问题是数据库中已经包含迁移和数据。

因此,在迁移默认用户模型后,您将AUTH_USER_MODEL更改为自定义模型。在运行迁移时,它将引发给定的错误。

由于您仍在开发中,而且它不是生产系统,因此解决此问题的最简单方法是清除数据库并再次运行迁移。

在这种情况下,另请参阅docs。还有一个有关如何创建自定义用户mid-project的主题。

© www.soinside.com 2019 - 2024. All rights reserved.