2016-10-11 2 views
2

저는 공장 관계자를 통해 장고 모델 세트와 다 대다 관계를 설정하는 데 문제가 있습니다. 나는 많은 요리법과 재료를 가지고있다. 수량을 설정하는 모델을 통해 Recipes와 Ingredients 간에는 다 대다 관계가 있습니다. 각 모델에 대한 공장을 가지고 있지만 연결할 수 없습니다.테이블을 통해 공장 소년에게 많은 동적 대다수를 설정하는 방법은 무엇입니까?

단순화 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

나는 factory-boy에 대한 문서가 다소 오도 된 것이라고 생각하지만, through 테이블과 많은 관계를 많이 인스턴스화하는 구체적인 지침은 실제로 위와 같이 할 수 있음을 나타내지는 않지만 실제로 할 수 있습니다. – bharling

+0

걱정하지 않아서 사용하기에 좋았습니다 :) – tdsymonds