2016-10-11 1 views
2

私は工場関係の男の子のdjangoモデルとの多対多の関係を設定する際に問題を抱えています。私はレシピや食材をたくさん持っています。数量を設定するモデルを通じて、レシピと成分の多対多の関係があります。私は各モデルの工場を持っていますが、それらをリンクさせることはできません。テーブルを介して工場の男の子で動的多対多を設定するにはどうすればよいですか?

簡素化models.py:

class Ingredient(models.Model): 
    name = models.CharField(max_length=40) 

class Recipe(models.Model): 
    name = models.CharField(max_length=128) 
    ingredients = models.ManyToManyField(Ingredient, through='RecipeIngredient') 

class RecipeIngredient(models.Model): 
    recipe = models.ForeignKey(Recipe) 
    ingredient = models.ForeignKey(Ingredient) 
    quantity = models.IntegerField(default=1) 

簡素化factories.py

class RecipeFactory(factory.django.DjangoModelFactory): 
    class Meta: 
     model = Recipe 

class IngredientFactory(factory.django.DjangoModelFactory): 
    class Meta: 
     model = Ingredient 

class RecipeIngredientFactory(factory.django.DjangoModelFactory): 
    class Meta: 
     model = RecipeIngredient 
    recipe = factory.SubFactory(RecipeFactory) 
    ingredient = factory.SubFactory(IngredientFactory) 
    quantity = 1 

私はfactory.RelatedFactoryをいじってみましたが、本当にどこにも持っていません。いずれかの側に多くの関係に多くを設定していないこのかかわらを行う

recipe = RecipeFactory(name="recipe1") 
ingredient = IngredientFactory(name="ingredient1") 
ri = RecipeIngredientFactory(recipe=recipe, ingredient=ingredient) 

、ともrecipeingredientモデル自体の作成に失敗するようだ:理想的には私は次の操作を行うことができるようにしたいです。誰かがこれを行う方法を知っていますか?

編集:

class RecipeWith3Ingredients(RecipeFactory): 
    ingredient1 = factory.RelatedFactory(RecipeIngredientFactory, 'recipe') 
    ingredient2 = factory.RelatedFactory(RecipeIngredientFactory, 'recipe') 
    ingredient3 = factory.RelatedFactory(RecipeIngredientFactory, 'recipe') 

をしかし、私は食材の既存のレシピとセットでこれらのオブジェクトを作成したいどのようにまわりで私の頭を取得カント:

私も試してみました。

答えて

2

私はこの設定を再作成しましたが、問題の内容を確認するのには苦労しています。すべてがうまくいくように見えるいくつかのテストはありますか?あるいは私はその質問を誤解していますか?

# create recipe ingredient and recipe ingredient 
recipe = RecipeFactory(name="recipe1") 
ingredient = IngredientFactory(name="ingredient1") 
recipe_ingredient = RecipeIngredientFactory(recipe=recipe, ingredient=ingredient) 

# recipe created?  
r = Recipe.objects.all().first() 
self.assertEqual(r, recipe) 

# ingredient created? 
i = Ingredient.objects.all().first() 
self.assertEqual(i, ingredient) 

# recipe ingredient created? 
ri = RecipeIngredient.objects.all().first() 
self.assertEqual(ri, recipe_ingredient)   

# test many to many 
self.assertEqual(ri, r.recipeingredient_set.all()[0]) 
self.assertEqual(ri, i.recipeingredient_set.all()[0]) 

# add a new ingredient to recipe 1 
ingredient2 = IngredientFactory(name='ingredient2') 
recipe_ingredient2 = RecipeIngredientFactory(recipe=recipe, ingredient=ingredient2) 

# test many to many 
self.assertTrue(recipe_ingredient in r.recipeingredient_set.all()) 
self.assertTrue(recipe_ingredient2 in r.recipeingredient_set.all()) 

# create a pre-existing recipe and a set of ingredients 
pizza_recipe = RecipeFactory(name='Pizza') 
cheese_on_toast_recipe = RecipeFactory(name='Cheese on toast') 

cheese_ingredient = IngredientFactory(name='Cheese') 
tomato_ingredient = IngredientFactory(name='Tomato') 
pizza_base_ingredient = IngredientFactory(name='Pizza base') 
toast_ingredient = IngredientFactory(name='Toast') 

# now put together 
RecipeIngredientFactory(recipe=pizza_recipe, ingredient=cheese_ingredient) 
RecipeIngredientFactory(recipe=pizza_recipe, ingredient=tomato_ingredient) 
RecipeIngredientFactory(recipe=pizza_recipe, ingredient=pizza_base_ingredient) 

RecipeIngredientFactory(recipe=cheese_on_toast_recipe, ingredient=cheese_ingredient)   
RecipeIngredientFactory(recipe=cheese_on_toast_recipe, ingredient=toast_ingredient)   

# test pizza recipe 
pizza_ingredients = [cheese_ingredient, tomato_ingredient, pizza_base_ingredient] 
pr = Recipe.objects.get(name='Pizza') 

for recipe_ingredient in pr.recipeingredient_set.all(): 
    self.assertTrue(recipe_ingredient.ingredient in pizza_ingredients) 

# test cheese on toast recipe 
cheese_on_toast_ingredients = [cheese_ingredient, toast_ingredient] 
cotr = Recipe.objects.get(name='Cheese on toast') 

for recipe_ingredient in cotr.recipeingredient_set.all(): 
    self.assertTrue(recipe_ingredient.ingredient in cheese_on_toast_ingredients) 

# test from ingredients side 
cheese_recipes = [pizza_recipe, cheese_on_toast_recipe] 
ci = Ingredient.objects.get(name='Cheese') 

for recipe_ingredient in ci.recipeingredient_set.all(): 
    self.assertTrue(recipe_ingredient.recipe in cheese_recipes) 
+0

ありがとうございました!それは私を少し助けてくれました - 私は実際には私の頭痛を引き起こしていた私の結合モデルでセーブメソッドを持っていたと思う、私はあなたが行っている驚くべき努力のために主にこれを受け入れています - ありがとう! - 賞金はあなたのものです。 – bharling

+0

私は工場の男の子のためのドキュメントは少し誤解していると思うが、スルーテーブルと多くの関係をインスタンス化するための具体的な指示は、あなたが上記のようにそれを行うことができることを実際に示していない。 – bharling

+0

心配しないで、それはいくつかの使用だったうれしい:) – tdsymonds

関連する問題