2016-07-05 76 views
0

我有以下Django模型:Django的许多一对多 '海峡' 对象有没有属性 '_meta'

class Recipe(models.Model): 
     ingredients = models.ManyToManyField(
      'foo.Ingredient', 
      through='RecipeIngredient', 
      through_fields=('recipe', 'ingredient') 
     ) 

class RecipeIngredient(models.Model): 

    recipe = models.ForeignKey(Recipe, blank=True, null=True) 
    ingredient = models.ForeignKey('foo.Ingredient', blank=True, null=True) 

将会产生一个错误:

name = model._meta.db_table 
AttributeError: 'str' object has no attribute '_meta' 

它看起来像ORM可以”找不到foo.Ingredient模型并对其进行解释像海峡(但ForeginKey正常工作)

我也尝试过使用through='current.RecipeIngredient,主要生产:

sec_column = fk.m2m_column_name() 
AttributeError: 'ManyToManyField' object has no attribute 'm2m_column_name' 

如何解决?

谢谢。

+1

此错误似乎相关:https://code.djangoproject.com/ticket/25292 – knbk

+0

Foo已安装。 – foo

+0

这几乎肯定与上面链接的bug报告有关,该报告仅在5天前关闭。如果可能的话,OP应该更新为绝对最新的Django,如果不是,则可能不得不停止使用'through_fields'。 @knbk应该使该评论成为答案。 –

回答

0

AttributeError: 'str'可以通过=“current_app.RecipeIngredient”与`替换through='RecipeIngredient'来解决(参见https://code.djangoproject.com/ticket/25292

AttributeError的:“ManyToManyField”对象没有属性“m2m_column_name”可以通过将成分与配方相同来解决应用程序。

但更新到Django 1.9.7是最好的方法。

相关问题