Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django cooking recipes site model structure

I'm working on Django website that should give a possibility to select cooking recipes containing the ingredients provided by user. So in brief, the idea of the site is "things you could make from the food in your refrigerator".

So I made 2 models

class Recipe (models.Model):
   name = models.CharField(max_length=255)
   ingredients = models.ManyToManyField(Ingredient)

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

Let's imagine, that I have as list ['egg','bread','meat','onion'].

Now I need select all Recipes that could be made from that list on ingredients. The problem is, that some recipes may have only some of ingredients from the list.
For example:

  • Egg Toast = egg + bread
  • Meat Egg Toast = meat + egg + bread
  • Meat with Onion = meat + onion
  • and etc...

So my question is: could it be possible to select all recipes that could be made from the list of the ingredients AND select the closest recipes that could be made from the list of ingredients + some ingredients from the shop?

For example: recipes has 3 elements from 4, so we add it to the result.

like image 533
Volodymyr Smirnov Avatar asked Oct 24 '22 10:10

Volodymyr Smirnov


1 Answers

Have you tried:

Receipt.objects.filter(ingredients__name__in=['egg','bread','meat','onion'])
like image 61
Sid Avatar answered Oct 27 '22 09:10

Sid