ValueError:字段admin.LogEntry.user是用惰性引用声明的。

问题描述 投票:2回答:3

我正在开发一个新的django项目,并且坚持迁移。

我想创建一个UserModel,到目前为止,我做了两件事。1.我制作了AuthUser模型,并在下面的类中设置了Meta类。

models.py中>

class AuthUser(AbstractUser):
    user_type_id = models.PositiveIntegerField(choices=UserTypes.choices())
    user_id = models.PositiveIntegerField()

    class Meta(AbstractUser.Meta):
        swappable = 'AUTH_USER_MODEL'

    @property
    def user_type(self):
        return UserTypes(self.user_type_id)

    def original_orm(self):
        if self.user_type.value == UserTypes.raijosha.value:
            return RaijoshaUsers.objects.filter(id=self.user_id).first()
        elif self.user_type.value == UserTypes.shuttennsha.value:
            return Users.objects.filter(id=self.user_id).first()
  1. 在settings.py中,我设置了AUTH_USER_MODEL。

AUTH_USER_MODEL ='recommend.AuthUser'

这是错误代码。

Operations to perform:
  Apply all migrations: admin, auth, contenttypes, recommend, recommend_raijousha, sessions
Traceback (most recent call last):
  File "manage.py", line 25, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.6/dist-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.6/dist-packages/django/core/management/__init__.py", line 355, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.6/dist-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.6/dist-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.6/dist-packages/django/core/management/commands/migrate.py", line 164, in handle
    pre_migrate_apps = pre_migrate_state.apps
  File "/usr/local/lib/python3.6/dist-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/usr/local/lib/python3.6/dist-packages/django/db/migrations/state.py", line 218, in apps
    return StateApps(self.real_apps, self.models)
  File "/usr/local/lib/python3.6/dist-packages/django/db/migrations/state.py", line 295, 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 'recommend.authuser', but app 'recommend' doesn't provide model 'authuser'.

您能帮助我们还是给我建议。

  Applying admin.0001_initial...Traceback (most recent call last):
  File "manage.py", line 25, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.6/dist-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.6/dist-packages/django/core/management/__init__.py", line 355, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.6/dist-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.6/dist-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.6/dist-packages/django/core/management/commands/migrate.py", line 204, in handle
    fake_initial=fake_initial,
  File "/usr/local/lib/python3.6/dist-packages/django/db/migrations/executor.py", line 115, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/usr/local/lib/python3.6/dist-packages/django/db/migrations/executor.py", line 145, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/usr/local/lib/python3.6/dist-packages/django/db/migrations/executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/usr/local/lib/python3.6/dist-packages/django/db/migrations/migration.py", line 129, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/usr/local/lib/python3.6/dist-packages/django/db/migrations/operations/models.py", line 97, in database_forwards
    schema_editor.create_model(model)
  File "/usr/local/lib/python3.6/dist-packages/django/db/backends/base/schema.py", line 254, in create_model
    definition, extra_params = self.column_sql(model, field)
  File "/usr/local/lib/python3.6/dist-packages/django/db/backends/base/schema.py", line 144, in column_sql
    db_params = field.db_parameters(connection=self.connection)
  File "/usr/local/lib/python3.6/dist-packages/django/db/models/fields/related.py", line 994, in db_parameters
    return {"type": self.db_type(connection), "check": self.db_check(connection)}
  File "/usr/local/lib/python3.6/dist-packages/django/db/models/fields/related.py", line 991, in db_type
    return self.target_field.rel_db_type(connection=connection)
  File "/usr/local/lib/python3.6/dist-packages/django/db/models/fields/related.py", line 909, in target_field
    return self.foreign_related_fields[0]
  File "/usr/local/lib/python3.6/dist-packages/django/db/models/fields/related.py", line 653, in foreign_related_fields
    return tuple(rhs_field for lhs_field, rhs_field in self.related_fields if rhs_field)
  File "/usr/local/lib/python3.6/dist-packages/django/db/models/fields/related.py", line 640, in related_fields
    self._related_fields = self.resolve_related_fields()
  File "/usr/local/lib/python3.6/dist-packages/django/db/models/fields/related.py", line 625, in resolve_related_fields
    raise ValueError('Related model %r cannot be resolved' % self.remote_field.model)
ValueError: Related model 'recommend.authuser' cannot be resolved

考虑到我的建议,我确实删除了迁移文件。但是发生了另一个错误,如上所示。

我正在开发一个新的django项目,并且坚持迁移。我想创建一个UserModel,到目前为止,我做了两件事。 1.我制作了AuthUser模型,并在...

python django migration
3个回答
3
投票

0
投票

0
投票
© www.soinside.com 2019 - 2024. All rights reserved.