2016-07-05 75 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是最好的方法。

相關問題