Django request using IN ()

I have a problem with a constructed model as follows:

class Topping(models.Model):
    name = models.CharField(max_length=30)

class Pizza(models.Model):
    cook = models.ForeignKey(Cook)
    name = models.CharField(max_length=50)
    toppings = models.ManyToManyField(Topping)

I am trying to select all the toppings that are used on the condition cook on his pizza.

in SQL, I could use it select * from Toppings t where t.id IN(select topping_id from Pizzas where cook_id = ?)or in some other way (certainly more efficient: P). The problem is that I have to use django models due to basic dependencies like swap.

Since this database was used by many applications, it would be great if it could be done without changing the models ...

+3
source share
1 answer
Topping.objects.filter(pizza__cook=mycook)
+4
source

All Articles