2012-05-04 2 views
0

좋아, 모델을 설정하는 방법에 대한 조언을 듣고 싶습니다. 나는 조리법 모델을 개발 중이다 (부엌에 타블렛을 놓을 계획). 제가 할 수 있기를 원하는 것은 각 제조법에 성분 목록이 있지만 성분 목록까지 일치하는 수량 목록이 있어야합니다. 아이디어는 내가 갖고있는 성분을 얼마만큼 추적 할 수있는 재료 모델을 가지고 있으며, 레시피 모델은 필요한 성분뿐만 아니라 성분 목록을 갖게 될 것입니다. 그렇게하면 앱에 내가 만들 재료가있는 조리법을 보여줄 수 있고 재료가없는 것들을 숨길 수 있습니다. (또는 실제로 더 정교 할 것입니다. 그러나 그 아이디어입니다.)Django 모델 필드

성분 model.py

class Unit_of_Measure(models.Model): 

    """Unit_of_Measure model. This is used as the foriegnkey for the Quantity model unit_of_measure key.""" 

    class Meta: 
      verbose_name_plural = "Units of Measure" 

    def __unicode__(self): 
      return self.unit_of_measure 

    unit_of_measure = models.CharField(max_length=200) 

class Location(models.Model): 

    """Location model. This is used a the foriegnkey for the Ingredient model location key.""" 

    class Meta: 
      verbose_name_plural = "Locations" 

    def __unicode__(self): 
      return self.place 

    place = models.CharField(max_length=200) 

class Ingredient(models.Model): 

    """Ingredients model. Includes ingredient title, quantity on hand, location of ingredient (foreignkey), expiration date, and if it is a shop for ingrdient.""" 

    class Meta: 
      verbose_name_plural = "Ingredients" 

    def __unicode__(self): 
      return self.title 

    title = models.CharField(max_length=200) 
    quantity = models.CharField(max_length=200) 
    unit_of_measure = models.ForeignKey(Unit_of_Measure) 
    location = models.ForeignKey(Location) 
    expiration_date = models.DateTimeField() 
    shop_for = models.BooleanField() 

레시피 model.py

class RecipeType(models.Model): 

    """Recipe type model. This is used as the foreign key for the Recipe model recipe style.""" 

    def __unicode__(self): 
      return self.style 

    style = models.CharField(max_length=200) 

class Recipe(models.Model): 

    """Recipe model. Includes recipe title, recipe style (dinner, snack, etc..), ingredient list (foreignkey), recipe instructions, storage style, and expiration date.""" 

    class Meta: 
      verbose_name_plural = "Recipes" 

    def __unicode__(self): 
      return self.title 

    title = models.CharField(max_length=200) 
    style = models.ForeignKey(RecipeType) 
    required_ingredient_list = models.ManyToManyField(Ingredient, related_name='additional_ingredient_list') 
    additional_ingredient_list = models.ManyToManyField(Ingredient, related_name='required_ingredient_list', blank=True) 
    recipe_instruction = models.TextField() 
    storage_style = models.CharField(max_length=200) 
    expiration_date = models.DateTimeField() 

그래서 두 개의 필드 목록을 일치하는 방법에 대한 어떤 제안 : 여기 내 현재 설정입니까? "required_ingredient_quantity_list"또는 무엇인가와 일치하는 "required_ingredient_list"를 좋아합니까? 또는 더 나은 해결책? 또는 일반적으로 제안? 지금은 재료별로 조리법을 분류 할 수 있지만 재료 모델의 양은 주방에있는 수량이므로 조리법에서 사용하는 양을 나타내는 필드가 실제로 없기 때문에 recipe_instruction 필드에서 간단히 말합니다. 할프 스!

답변

0

사용하십시오 "through model"은 성분에 조리법을 연결합니다. 그런 다음 중간 모델에는 연관된 단위와 그와 연관된 숫자 또는 문자열 측정 값이있을 수 있습니다.

+0

허. models.Recipe 필드를 추가 (I 모델을 통해 같은 레시피 후하는 recipeIngredient 모델을 추가했지만 아무것도 관리자 페이지에 표시되지 않습니다 "ingredient_list = models.ManyToManyField (= 'recipe.RecipeIngredient'을 통해 'ingredient.Ingredient',") noooothing는에 보여줍니다 ingredient_list에 대한 관리자 페이지. 어떤 아이디어? – jonc

+0

은 신경 끄시 고! 난 그냥 인라인으로 RecipeAdmin로하는 recipeIngredient 모델을 추가했다, 그것을 얻었다. 지금은 인라인으로 레시피 페이지 레시피 성분을 표시합니다. 달콤한. – jonc