[OK]を、私はモデルを設定する方法のいくつかのアドバイスをしたいと思います。私は開発中のレシピモデルを持っています(台所に錠剤を入れる計画)。私ができることを望むのは、各レシピに成分リストがあり、それに対応する成分リストが成分リストに一致するようにすることです。アイデアは、私が手元に持っている成分とその量を把握するために使用される成分モデルがあり、レシピモデルはそれ自体の成分リストと必要な量があることです。そのようにすれば、私は作る材料を持っているレシピを見せることができ、材料を持たないものを隠すことができます(またはもっと精巧になりますが、それはアイデアです)。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()
だから、2つのフィールドのリストを一致する方法に関していかなる提案:ここに私の現在の設定は? "required_ingredient_quantity_list"などと一致する "required_ingredient_list"のようなものですか?それとも良い解決策ですか?または一般的な提案ですか?今は材料でレシピを並べ替えることができますが、材料モデルの量はキッチンで手に取っている量なので、レシピが使用する量を表すフィールドは実際にはありません。単にrecipe_instructionフィールドで述べられます。ハルツ!
huh。 RecipeIngredientモデルをスルーモデルとして追加しましたが、管理ページには何も表示されません(models.Recipeフィールドに「ingredient_list = models.ManyToManyField( 'ingredient.Ingredient'、through = 'recipe.RecipeIngredient'」)。どのようなアイデアを持っているのでしょうか? – jonc
nevermind!私はそれを取得しました。レシピエントリーモデルをレシピページにインラインで表示するようになりました。 – jonc