Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ValueError: The field admin.LogEntry.user was declared with a lazy reference

I'm developing a new django project and stuck with migration.

I would like to make a UserModel, and in order to do that, I did two things so far. 1. I made AuthUser model and set a Meta class in the class below.

In 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. In settings.py, I set AUTH_USER_MODEL.

    AUTH_USER_MODEL = 'recommend.AuthUser'

Here's the error codes.

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'.

Could you please help us or give me an advise.

  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

Considering an advise, I did delete migration files. But another error occured shown above.

like image 726
Hodaka Kubo Avatar asked May 14 '18 06:05

Hodaka Kubo


1 Answers

I delete all the migration files and database and applied it.

Then I could migrate.

like image 55
Hodaka Kubo Avatar answered Nov 15 '22 19:11

Hodaka Kubo