1

This is a follow-up question to Creating a Django Model for a recipe. I am able to select multiple ingredients for a single recipe, but my code only allows for one general quantity selection that associates with all ingredients selected. For example: (BLT recipe) I can select Bacon, Lettuce, and Tomato, but I am not able to have different quantities for each (i.e. Bacon(1), Lettuce(1), Tomato(2).

class Recipe(models.Model):
    '''A recipe class to input new recipes'''
    recipe = models.CharField(max_length=50)
    ingredient = models.ManyToManyField(Ingredient)
    quantity = models.CharField(max_length=1)
    cuisine = models.ForeignKey(Cuisine, null=True, on_delete=models.SET_NULL)

    def __str__(self):
        return self.recipe

class Ingredient(models.Model):
    '''All ingredients for any recipe or individual selection'''
    ingredient = models.CharField(max_length=50)
    department = models.ForeignKey(Department, null=True, on_delete=models.SET_NULL)

    def __str__(self):
        return self.ingredient

1 Answers1

0

my code only allows for one general quantity selection that associates with all ingredients selected

class Recipe(models.Model):
    '''A recipe class to input new recipes'''
    recipe = models.CharField(max_length=50)
    ingredient = models.ManyToManyField(Ingredient)
    quantity = models.CharField(max_length=1)
    cuisine = models.ForeignKey(Cuisine, null=True, on_delete=models.SET_NULL)

    def __str__(self):
        return self.recipe

Contrary to what you describe in English, you associate quantity with a recipe, not with ingredients. Since it doesn't make sense for there to be one quantity per recipe, quantity doesn't belong in the Recipe class. Instead, quantity should be in the Ingredient class because each ingredient has a quantity.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • Ahh, I see what you're saying now. Thank you! I think I was looking at the class incorrectly. I will poke around a little and see where I get. However, I still don't understand how associating a single quantity for an ingredient will help with additional recipes, say, one that only requires (1) tomato? – HoundDogSaw Apr 26 '22 at 19:37
  • @HoundDogSaw Say you have two recipes, one which requires 1 tomato and one which requires 2 tomatoes. You should treat these as two *different* ingredients. This is easy to do if you use the `ForeignKey` suggestion in my answer to your previous question. Then you create `recipe1` and `recipe2` for `recipe1`, you create an ingredient object that represents 1 tomato with a foreign key to `recipe1`. Similarly, you create another ingredient object that represents 2 tomatoes with a foreign key to `recipe2`. – Code-Apprentice Apr 26 '22 at 19:55
  • @HoundDogSaw Notice how in code, an "ingredient" is a combination of the quantity and the type of thing it is. This is different than the literal meaning of the word "ingredient" in English which is just the type of thing and doesn't include the quantity. If it makes sense, you can separate out the "things" and the "amount of a thing" as separate models. In this particular situation, doing so would add unnecessary complexity, so I wouldn't go down that road. – Code-Apprentice Apr 26 '22 at 19:56
  • After thinking about it a bit more, you **could** associate a "quantity" with an ingredient for a specific recipe with `ManyToManyField` by explicitly declaring a model for the `ManyToMany` relationship and using [`through`](https://docs.djangoproject.com/en/4.0/ref/models/fields/#django.db.models.ManyToManyField.through). – Code-Apprentice Apr 26 '22 at 20:02
  • Thank you so much for the feedback. Ill need some time to research some of these options, but I definitely see where you're going with this. Much appreciated! I will reply again if/when Im able to get some more code down. – HoundDogSaw Apr 26 '22 at 20:27
  • So, I'm currently trying to implement your suggestion with a ManyToManyField using 'through'. I keep getting an error that my intermediate model doesn't include a foreign key to the Ingredient model, and it certainly does. Im not sure what im missing. – HoundDogSaw May 12 '22 at 21:13
  • @HoundDogSaw Please post a new question with the current version of your code. I can't begin to guess what the problem might be without seeing exactly what you are doing. – Code-Apprentice May 12 '22 at 22:01
  • I apologize, I should know better by now... https://stackoverflow.com/questions/72231338/creating-a-django-model-for-a-recipe-3 – HoundDogSaw May 13 '22 at 14:39