10

I am trying create a new model with Django, but I keep running into the error Lookup failed for model referenced by field help.HelpDefinition.org: account.Organization. Organization has been imported. You can see the model below.

models.py

org = models.ForeignKey(Organization, unique=True)
help_type = models.CharField(max_length=255, choices=HELP_CHOICES)
help_content = models.TextField(blank=True)

This model has been successfully migrated previously. I dropped the table via psql in Postgres so that it could be recreated.

3 Answers 3

19

It happens when you change the target objects in relationship. Even if they have the same name and fields they are not the same objects. I had the same issue and deleting all previous migrations from migrations folder solved it.

2
  • 1
    This sounds weird, but is in fact the way to do it. Modifications to model names seems something Django migrations do not support yet (or did not support yet in version 1.7.3). Commented Apr 3, 2015 at 9:47
  • If you need to modify the name of a model, you may create an empty migration and use RenameModel class.
    – warownia1
    Commented Jun 5, 2015 at 12:05
4

You can also add as a dependency to the migration the last migration from the object's app. That did the trick for me.

class Migration(migrations.Migration):

dependencies = [
    (<app>, <last_migration_filename>),
...
2
  • I also solved the "Lookup failed for model referenced by field..." error by adding another app migration in dependencies. Commented Sep 27, 2017 at 15:36
  • Not all heroes wear capes. This answer saved me. Top marks!
    – drew
    Commented Jun 1, 2018 at 9:24
0

My case was: moving away from South I deleted almost all migration files from several apps and applied makemigrations and migrate and later on I found out some forgotten migrations in one app and I tried to do the process (delete/makemigrations) only for this one app. But going back one step and recreating the migrations for ALL the apps fixed the issue for me.

Not the answer you're looking for? Browse other questions tagged or ask your own question.