[Django]-Django ValueError: 'dtedeclared with a lazy reference to …..' when changed Model Name

3👍

the solution is to access the django_migrations table in your database and delete the migration that led to this error. Then go to the your specific app migrations folder and delete the particular migration

this is the last option if
./manage.py migrate specific_app <specific_migration_to_rollback_to> is not working, its a common issue in django version 1.10

6👍

Just been truggling with such a deadlock myself, keep getting “ValueError: The field … was declared with a lazy reference to ‘…’, but app ‘…’ doesn’t provide model ‘…’.”.

After diving into pgadmin and removing/recovering some tables i actually found that it could also be resolved by adding a new migration that tells django that the referred model has changed:

    migrations.AlterField(
        model_name='...',
        name='...',
        field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='...', to='<app>.<new-model>'), <===
    ),

In addition i had to set dependencies across apps right, so for instance the AlterField should be dependent on the migration that created that model obviously. Don’t ask me why it did not work out-of-the-box, i just had to go through this….

Paul

1👍

My problem was that PyCharm’s refactor got a bit aggressive and changed the model reference in the actual migrations files. Reverted the changed migrations and was able to proceed.

In case this helps anybody with newer versions of Django that can usually handle model renames without issue.

0👍

This problem is resolved by Django 1.11. (Or at least, it was for me.)

Leave a comment