0

好的,所以我想要一些关于如何设置模型的建议。我有一个正在开发的食谱模型(计划在厨房里放一个平板电脑)。我想做的是让每个食谱都有一个成分表,但也有一个与成分表匹配的相应数量列表。我的想法是我有一个成分模型,用于跟踪我手头有哪些成分和多少,配方模型将有它自己的成分列表以及需要的数量。这样我就可以让应用程序显示我有配料要做的食谱,并隐藏我没有配料的食谱(或者实际上它会更精细,但就是这样)。这是我当前的设置:

成分模型.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()

配方模型.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_list”匹配到“required_ingredient_quantity_list”之类的?还是更好的解决方案?或者一般有什么建议?现在我可以按成分对食谱进行排序,但由于成分模型的数量是我厨房里手头的数量,我真的没有一个字段来表示食谱使用的数量,它只是在 recipe_instruction 字段中说出来。哈尔普兹!

4

1 回答 1

0

使用“通过模型”将食谱链接到成分。然后,中间模型可以具有关联的单位和与之关联的数字或字符串度量。

于 2012-05-04T03:23:22.463 回答