0
votes

I'm creating a small app which essentially counts calories in recipes. I'm curious to know if there is a better way to do this. Each recipe has ingredients. Each ingredient has an amount, a weight, and a calorie profile.

So for instance:

  • Recipe: Flourless Nutella Cake
    • Ingredient: Eggs
      • Amount: 4
      • Weight: Large eggs (could be cups or oz or whatever)
      • Calories: 312 (calculated essentially from Amount * Weight)
    • Ingredient: Nutella
      • Amount: 8.5
      • Weight: ounces
      • Calories: 850

Here is models.py:

from FoodList.models import Food, Weight

class Ingredient(models.Model):

food = models.ForeignKey(Food)
weight = models.ForeignKey(Weight)
amount = models.FloatField(default=1.0)

...

class Recipe(models.Model):

ingredients = models.ManyToManyField(Ingredient, blank=True, null=True)

...

The problem is that every time I want to make a new Recipe with a different amount of eggs, I first have to make a new ingredient containing the correct amount of eggs. So I have one ingredient that says "4 large eggs" and another ingredient that says "2 large eggs."

I'm currently using inlines in my admin to see it all together, but it still seems rather cumbersome and like there could be a better way.

Is there any way that I can do everything under recipe instead of an intermediate step in Ingredient?

1

1 Answers

0
votes

Remove the amount field from the Ingredients model, and create a new model like this:

class Amount(models.Model):
    units = model.IntegerField(default=1)
    ingredient = models.ForeignKey(Ingredient)
    recipe = models.ForeignKey(Recipe)

As long as you keep the amounts separate from the ingredients model, you can put all your ingredients in and then track amounts using the "units" field for each recipe.