Let's say I have three models - Person, Food, Flavor.
Person and Flavor share a many-to-many relationship (a person may like many tastes).
Food and taste share many-to-many relationships (food can have many tastes).
For any given person, I would like to return all products that have a set of aromas, which is a subset of a person’s tastes.
For instance,
personA.flavor.all () → ['spicy', 'sweet', 'bitter'] foodA.flavor.all () → ['spicy', 'sweet', 'bitter
]
foodB.flavor.all () → [' spicy, 'bitter']
foodC.flavor.all () → ['bitter', 'bold']
I would like to use the Django filter in Food.objects so that it returns a QuerySet containing foodA and foodB.
I understand that I can achieve a similar result if I convert user tastes into a set and call issuperset for each of the taste lists (and add such food to a predefined list), but I am interested (and hoping) if there is a more elegant a solution that includes the filter function of the django model is what will return a QuerySet to me instead of a list.
Thanks in advance for your help.
jang0 source
share