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.
I think I found one solution. Using the code
I’m able to select all possible combinations of ingredients from the lists.
Gives me result
Now the problem is how to optimize the query so I can select all recipes containing the list of this ingredient. In MongoDB I was using
Is there any alternative solution for MySQL?