I ve got the following model structure:
class Pizza(models.Model):
name = models.CharField(max_length=50)
shop = models.CharField(max_length=50)
class Topping(models.Model):
name = models.CharField(max_length=50)
pizza = models.ManyToManyField(Pizza)
I want to find all pizzas which are with ham or cheese or both, is this the standard django way?: from django.db.models import Q
Pizza.objects.filter(Q(topping__name='ham') | Q(topping__name='cheese'))
How do i do it programatically, if i got a list of toppings, e.g?:
['ham','cheese']
You could do it with dicts and **kwargs, but there's no need: easier to just use __in
:
topping_list = ['ham', 'cheese']
Pizza.objects.filter(topping__name__in=topping_list)