我有两个应用程序(比如ookeek),并且我想使用ook中的模型的eek中的模型的外键。两者都在INSTALLED_APPS中,首先是ook

ook.models.py中,我有:

class Fubar(models.Model):
    ...

eek.models.py中,我有:
class monkey(models.Model):
    external = models.ForeignKey('ook.Fubar', blank=True, null=True)
    ...

生成的迁移是:
class Migration(migrations.Migration):

    dependencies = [
        ('eek', '0002_auto_20151029_1040'),
    ]

    operations = [
        migrations.AlterField(
            model_name='monkey',
            name='external',
            field=models.ForeignKey(blank=True, to='ook.Fubar', null=True),
        ),
    ]

运行迁移时,出现以下错误:
   ...
   1595             raise ValueError('Foreign Object from and to fields must be
the same non-zero length')
   1596         if isinstance(self.rel.to, six.string_types):
-> 1597             raise ValueError('Related model %r cannot be resolved' % self.rel.to)
   1598         related_fields = []
   1599         for index in range(len(self.from_fields)):
ValueError: Related model u'ook.Fubar' cannot be resolved

我在做什么错?

最佳答案

因为您在操作中具有ForeignKey,所以必须将ook添加到dependencies:

dependencies = [
    ('ook', '__first__'),
    ('eek', '0002_auto_20151029_1040'),
]

Django迁移具有两个“魔术”值:
  • __first__-获取模块首次迁移
  • __latest__-获取模块最新迁移
  • 关于django - ValueError:无法解析相关模型u'app.model',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33496333/

    10-12 13:24