How to handle recipe ingredients in database

Issue

Hi I’m new to programming and I am building a recipe website to learn, what I am struggling with though is how to handle recipe ingredients? I would like to do the following:

  • Have global recipe ingredients i.e. common ingredients, chicken, beef etc.
  • Allow users to create their own ingredients (for that user only) i.e. big tomato
  • Attach ingredients to a recipe regardless of if they are global or user created
  • Allow users to add ingredients to their pantry and how much of the ingredient they have in stock

What I think the models would like is below, but I’m not sure if this is correct or the best method, any advice appreciated.

recipe/models.py

User = settings.AUTH_USER_MODEL

class Recipe(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    recipeName = models.CharField(max_length=220) # grilled chicken pasta
    userRecipeIngredients = models.ManyToManyField(UserCreatedIngredient, blank=True, Null=True, through='IngredientToRecipe')
    globalRecipeIngredients = models.ManyToManyField(GlobalIngredient, blank=True, Null=True, through='IngredientToRecipe')


class UserCreatedIngredient(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    name = models.CharField(max_length=220) # grilled chicken

class GlobalIngredient(models.Model):
    name = models.CharField(max_length=220) # chicken

class IngredientToRecipe(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    userIngredient = models.ForeignKey(UserCreatedIngredient, on_delete=models.SET_NULL)
    globalIngredient = models.ForeignKey(GlobalIngredient, on_delete=models.SET_NULL)
    quantity = models.CharField(max_length=50, blank=True, null=True) # 400
    unit = models.CharField(max_length=50, blank=True, null=True) # pounds, lbs, oz ,grams, etc
    instructions = models.TextField(blank=True, null=True) # chopped, diced etc.

pantry/models.py:

from recipes.models import IngredientToRecipe

User = settings.AUTH_USER_MODEL 

class pantryIngredients(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    ingredients = models.ForeignKey(IngredientToRecipe, on_delete=models.SET_NULL)
    inStock = = models.IntegerField(default=0, blank=False) # user can increment i.e. two chicken in stock

Solution

One way to address the first three requirements in your question would be to build inheritance into your models.

class Recipe(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    recipeName = models.CharField(max_length=220) # grilled chicken pasta
    ingredients = models.ManyToManyField(Ingredient, blank=True, Null=True, through='IngredientToRecipe')

class Ingredient(models.Model):
    name = models.CharField(max_length=220)

class GlobalIngredient(Ingredient):
    pass

class UserCreatedIngredient(Ingredient):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)

class IngredientToRecipe(models.Model):
    ingredient = models.ForeignKey(Ingredient, on_delete=models.SET_NULL)
    quantity = models.CharField(max_length=50, blank=True, null=True) # 400
    unit = models.CharField(max_length=50, blank=True, null=True) # pounds, lbs, oz ,grams, etc
    instructions = models.TextField(blank=True, null=True) # chopped, diced etc.

By allowing this inheritance to take place you can improve data redundancy.

Answered By – Lewis

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published