gpt4 book ai didi

python - ValueError : The field admin. LogEntry.user 是用惰性引用声明的

转载 作者:行者123 更新时间:2023-12-03 15:57:42 26 4
gpt4 key购买 nike

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

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

模型.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()
  • 在 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

    考虑到建议,我确实删除了迁移文件。
    但是上面显示了另一个错误。

    最佳答案

    如果您运行默认值 auth,就会发生这种情况应用程序迁移和后来更改了 AUTH_USER_MODELsettings.py .您可以尝试以下操作:

    # comment AUTH_USER_MODEL in settings.py so it points to default User model

    python manage.py migrate auth zero

    # uncomment to be AUTH_USER_MODEL='recommend.AuthUser'

    python manage.py migrate auth

    关于python - ValueError : The field admin. LogEntry.user 是用惰性引用声明的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50324561/

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