【问题标题】:Django model fieldsDjango 模型字段
【发布时间】:2012-05-13 14:30:03
【问题描述】:

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

成分模型.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 字段中说出来。哈尔普兹!

【问题讨论】:

    标签: django


    【解决方案1】:

    使用"through model" 将食谱链接到配料。然后,中间模型可以有一个关联的单位和一个与之关联的数字或字符串度量。

    【讨论】:

    • 嗯。我在 Recipe 之后添加了一个 RecipeIngredient 模型作为直通模型,但管理页面上没有显示任何内容(models.Recipe 添加字段“ingredient_list = models.ManyToManyField('ingredient.Ingredient', through='recipe.RecipeIngredient'") noooothing 显示成分列表的管理页面。有什么想法吗?
    • 没关系!我明白了,只需将 RecipeIngredient 模型作为内联添加到 RecipeAdmin。现在它将配方页面上的配方成分显示为内联。甜。
    猜你喜欢
    • 2011-06-22
    • 2019-12-24
    • 2013-02-03
    • 2018-01-01
    • 2011-02-23
    • 2019-12-31
    • 2013-04-09
    • 2011-03-10
    • 2012-03-23
    相关资源
    最近更新 更多