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

Related searches

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.

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

Then I could migrate.

lazy reference: doesn't provide model user?, msg for error in errors)) ValueError: The field admin.LogEntry.user was declared with a lazy reference to 'accounts.user',� ValueError: The field admin.LogEntry.user was declared with a lazy reference The field admin.LogEntry.user was declared with a lazy reference to 'recommend

It happens if you ran default auth app migrations and later changed the AUTH_USER_MODEL in settings.py. You can try following:

# 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

ValueError: The field admin.LogEntry.user | by Hassan Raza, ValueError: The field admin.LogEntry.user was declared with a lazy reference to ' accounts.user', but app 'accounts' doesn't provide model� The field saba_dashboard.Authentication_ip.ipaddresses was declared with a lazy reference to 'core.ipaddresses', but app 'core' doesn't provide model 'ipaddresses'. How do I resolve this? I'm using: Django 1.10.5 Postgresql Python 2.7

For me helped split on two migration

  1. create new table (with out connection betwin new and old tables and without AUTH_USER_MODEL = 'recommend.authuser')
  2. add AUTH_USER_MODEL to settins.py and other connections with new table

#22563 (Migration of admin.LogEntry.user fails.) – Django, ValueError: Lookup failed for model referenced by field admin. Any migration that references the User model should have this auto-added. it looks like django migrations don't support reverse dependencies (i.e. declaring that a third- party� I can reproduce it too, with Django 1.7.x checked out a few hours ago, with a custom User model inheriting directly from Abstract User. Trying to run tests with manage.py test also raises the same exception, and the same workaround works for both migrations and tests.

The field admin.LogEntry.user was declared with a lazy reference to , The field admin.LogEntry.user was declared with a lazy reference to 'account. user', but app 'account' doesn't provide model 'user'� The field oauth2_provider.Grant.application was declared with a lazy reference to 'app.brownpaperapplication', but app 'app' doesn't provide model 'brownpaperapplication'. The field oauth2_provider.RefreshToken.application was declared with a lazy reference to 'app.brownpaperapplication', but app 'app' doesn't provide model 'brownpaperapplication'.

Getting Errors When Running "Makemigrations". : django, ValueError: The field admin.LogEntry.user was declared with a lazy reference to ' login.stylista', but app 'login' isn't installed. The field authtoken� The field djstripe.Customer.subscriber was declared with a lazy reference to 'myapp.myuser', but app 'myapp' isn't installed. Hopefully I haven't done anything terribly stupid/made an obvious mistake here.

lazy reference: doesn't provide model user?, raise ValueError("\n".join(error.msg for error in errors)) ValueError: The field admin.LogEntry.user was declared with a lazy reference to 'accounts.user', but app� ValueError: The field admin.LogEntry.user was declared with a lazy reference to 'login.stylista', but app 'login' isn't installed. The field authtoken.Token.user was declared with a lazy reference to 'login.stylista', but app 'login' isn't installed.

Comments
  • Possible duplicate of lazy reference: doesn't provide model user?
  • @Hadaka Kubo delete your database and migrations and create db and makemigrations and apply it.
  • @AnjaneyuluBatta I deleted my database and applied it. But another error occured. It said "ValueError: Related model 'recommend.authuser' cannot be resolved" So im trying to delete all the migration files.
  • @AnjaneyuluBatta I could solve this problem! Thank you for your advise!
  • Haha, this wild operation worked for me. Delete all tables and migration files and then recreate migrations and apply. Only works for project in the early development phrase which has no serious data. Don't try this on you production project.
  • For me, only deleting migrations files was enough. It was not necessary to delete all tables. Thank you!