相关模型无法解决

时间:2015-03-04 18:45:57

标签: python django

我正在使用Django 1.8b1

两个应用中有两个模型,分别为accountsproducts

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

另一个模型是

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

    def __unicode__(self):
        return self.enterprise_type

当我python manage.py makemigrations时,它会添加迁移文件。但当我python manage.py migrate时,它会引发我的错误:

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

我该如何解决这个问题。

赞赏答案:)

2 个答案:

答案 0 :(得分:1)

一个非常延迟的回复...但我刚刚遇到了类似的问题很难追查,但最终确实如此,所以我在这里放一个指针以防万一其他人被它击中。我正在为几年前的Django webapp添加测试,发现./manage.py测试失败了。我从未在空数据库上运行./manage.py迁移!

在Django 1.7早期的某个阶段,生成了循环依赖项检测失败的迁移,因此您可以从较新版本更新而不是第一次迁移。有关错误报告,请参阅https://code.djangoproject.com/ticket/22319

我仍然需要弄清楚如何修复它而不丢弃所有迁移并生成它们。

为了解决这个问题,我完成了所有迁移(只有5个受影响的文件)。早期迁移需要某些模型,但由于各个字段的不同,它们不包括在内,具体取决于以后的迁移。因此,我将模型的整个migrations.CreateModel带回到早期的迁移,但是在后面的迁移中(在最初放置模型的地方)中使用了这些字段并使用了migrations.AddField

希望这能解释它,但如果有人在将来某个时候遇到这个问题,请随时评论是否需要进一步阐述。

答案 1 :(得分:0)

我认为这一行:

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

应该是:

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