字段admin.LogEntry.user是用对“app2.user”的惰性引用声明的,但应用程序“app2”不提供模型“user”

2024-03-28 18:35:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试在Django中构建一个定制的用户模型。我的应用程序名是app2

我的models.py

from django.contrib.auth.models import AbstractUser

class User(AbstractUser):
    is_Employee = models.BooleanField(default=False)
    is_Inspector = models.BooleanField(default=False)
    is_IndustryOwner = models.BooleanField(default=False)
    is_Admin = models.BooleanField(default=False)
    
class Employee(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
    extraField = models.TextField(blank=True)

我已经把AUTH_USER_MODEL = 'app2.User'放在我的settings.py里了

admin.py中:

admin.site.register(User)
admin.site.register(Employee)

现在,当我尝试迁移时,出现以下错误:

Operations to perform:
  Apply all migrations: admin, app2, auth, contenttypes, sessions
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 "G:\Python\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "G:\Python\lib\site-packages\django\core\management\__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "G:\Python\lib\site-packages\django\core\management\base.py", line 328, in run_from_argv
    self.execute(*args, **cmd_options)
  File "G:\Python\lib\site-packages\django\core\management\base.py", line 369, in execute
    output = self.handle(*args, **options)
  File "G:\Python\lib\site-packages\django\core\management\base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "G:\Python\lib\site-packages\django\core\management\commands\migrate.py", line 190, in handle
    pre_migrate_apps = pre_migrate_state.apps
  File "G:\Python\lib\site-packages\django\utils\functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "G:\Python\lib\site-packages\django\db\migrations\state.py", line 209, in apps
    return StateApps(self.real_apps, self.models)
  File "G:\Python\lib\site-packages\django\db\migrations\state.py", line 279, in __init__
    raise ValueError("\n".join(error.msg for error in errors))
ValueError: The field admin.LogEntry.user was declared with a lazy reference to 'app2.user', but app 'app2' doesn't provide model 'user'.

我怎样才能解决这个问题


Tags: djangoinfrompycoreselfexecutemodels