5

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.

0

2 Answers 2

3

Have you tried:

Receipt.objects.filter(ingredients__name__in=['egg','bread','meat','onion'])
11
  • 1
    Should that be ingredients__name__in=['egg', 'bread', 'meat', 'onion']? Feb 1, 2012 at 16:27
  • Good catch! Just fixed. Thanks :)
    – Sid
    Feb 1, 2012 at 16:29
  • yes, I tried that from the start, but the problem is that it gives me very large list without any ordering and sorting. There are a lots of recipes made from eggs and none made from egg + bread. Feb 1, 2012 at 16:33
  • Ah, so in other words it gives you recipes with at least one of the ingredients, not all (or all less one, etc.)? Feb 1, 2012 at 16:34
  • You could chain filters and use individual elements for e.g.: Receipt.objects.filter(ingredients__name='egg').filter(ingredients__name='bread').... Obviously a messy solution but can't think of any other. What DB are you using? Can you resort to raw sql?
    – Sid
    Feb 1, 2012 at 16:38
0

I think I found one solution. Using the code

from itertools import chain, combinations
def all_subsets(ss):
    return chain(*map(lambda x: combinations(ss, x), range(0, len(ss)+1)))

I'm able to select all possible combinations of ingredients from the lists.

for s in all_subsets(['egg','bread','meat','onion']):
    if len(s)>2:
        print s

Gives me result

('egg', 'bread', 'meat') ('egg', 'bread', 'onion') ('egg', 'meat', 'onion') ('bread', 'meat', 'onion') ('egg', 'bread', 'meat', 'onion')

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

receipts.find({'ingredients.name':{'$all':ingredients_list}})

Is there any alternative solution for MySQL?

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.