本文介绍了ValueError:无法解析相关模型u'app.model'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have two applications (ook and eek say) and I want to use a foreign key to a model in ook from a model in eek. Both are in INSTALLED_APPS with ook first.

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),
        ),
    ]

运行迁移时,出现以下错误:

When I run the migration, I get this error:

   ...
   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 添加到依赖项

Because You have ForeignKey in operations, You must add a ook to dependencies:

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

Django迁移具有两个魔术值:

Django migrations have two "magic" values:


  • __ first __ -获取模块优先迁移

  • __ latest __ -获取模块最新迁移

  • __first__ - get module first migration
  • __latest__ - get module latest migration

这篇关于ValueError:无法解析相关模型u'app.model'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 15:06