Django模型字段

时间:2012-05-04 03:06:45

标签: 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_list”匹配“required_ingredient_quantity_list”或什么?还是更好的解决方案?或者一般的建议?现在我可以按成分对食谱进行分类,但由于成分模型的数量是我在厨房里的数量,我实际上没有一个字段来表示食谱使用的数量,它只是在recipe_instruction字段中说出来。 Halpz!

1 个答案:

答案 0 :(得分:0)

使用"through model"将食谱与食材相关联。然后,中间模型可以具有关联的单元以及与之关联的数字或字符串度量。