Ok, so I would like some advice on how to set up a model. I have a recipe model I’m developing (plan on putting a tablet in the kitchen). What I would like to be able to do is have each recipe have an ingredients list, but also have a corresponding quantity list that matches up to the ingredient list. The idea is that I have an Ingredient model that will be used to keep track of what ingredients I have on hand and how much, and the Recipe model will have it’s own ingredients list as well as what quantities are needed. That way I could have the app show recipes that I have ingredients to make, and hide ones I don’t have ingredients to (or actually it will be more elaborate but that’s the idea). Here’s my current setup:
Ingredient 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()
Recipe 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()
So any suggestion as to how to match up two field lists? like “required_ingredient_list” matching up to “required_ingredient_quantity_list” or something? Or a better solution? Or any suggestions in general? Right now I can sort recipes by ingredients but since the Ingredient model’s quantity is quantity I have on hand in my kitchen, I don’t really have a field to represent the quantities the recipe uses, it just gets said in the recipe_instruction field. Halpz!
Use a “through model” to link Recipes to Ingredients. The intermediate model could then have an associated unit and a numeric or string measure associated with that.