Yashwanth Babu
Yashwanth Babu

Reputation: 939

Related model cannot be resolved

I am using Django 1.8b1

There are two models in two apps called accounts and products

products/models.py
class ChecklistEnterpriseType(models.Model):
    checklist_enterprise_type = models.CharField('Type of Enterprise', max_length=50, choices=zip(ENTERPRISE_CHOICES, ENTERPRISE_CHOICES))

    def __unicode__(self):
        return self.checklist_enterprise_typ

And the another model is

accounts/models.py
class sample(models.Model):
    enterprise_type = models.ForeignKey(ChecklistEnterpriseType, related_name='enterprise_type')

    def __unicode__(self):
        return self.enterprise_type

When I do python manage.py makemigrations, it will add the migration file. But when I do python manage.py migrate it raises me the error like:

raise ValueError('Related model %r cannot be resolved' % self.rel.to)
ValueError: Related model u'products.ChecklistEnterpriseType' cannot be resolved

How can I resolve this.

Appreciated the answers :)

Upvotes: 4

Views: 8392

Answers (2)

neomanic
neomanic

Reputation: 366

A very delayed response... but I just had a similar issue that was very hard to track down, but eventually did, so am putting a pointer here in case anyone else gets hit by it. I was adding testing to a several year old Django webapp, and found that ./manage.py test was failing. I had never run ./manage.py migrate on an empty database!

At some stage in the early days of Django 1.7 migrations were generated that had failed circular dependency detection, so you could update from a newer version but not the first migrations. See https://code.djangoproject.com/ticket/22319 for the bug report.

I still had to work out how to fix it without throwing away all my migrations and generating them fresh.

So to fix, I went through all the migrations (it was only 5 affected files). Some models were required by earlier migrations, but they weren't included due to individual fields depending on later migrations. So I brought the whole migrations.CreateModel for the model back to an earlier migration, but took those fields and used migrations.AddField in the later migration (where the model had been put originally).

Hopefully that explains it, but if anyone runs into this issue sometime in the future, feel free to comment if it needs further elaboration.

Upvotes: 3

Du D.
Du D.

Reputation: 5310

I think this line:

 checklist_enterprise_type = models.CharField('Type of Enterprise', max_length=50, choices=zip(ENTERPRISE_CHOICES, ENTERPRISE_CHOICES))

Should Be:

 checklist_enterprise_type = models.CharField(verbose_name='Type of Enterprise', max_length=50, choices=zip(ENTERPRISE_CHOICES, ENTERPRISE_CHOICES))

Upvotes: 0

Related Questions