ValueError:无法解析相关模型 u'app.model' [英] ValueError: Related model u'app.model' cannot be resolved

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

问题描述

我有两个应用程序(比如 ookeek),我想使用来自模型的 ook 模型的外键在 eek 中.两者都在 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,你必须在dependencies中添加一个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'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆