tags:

views:

46

answers:

1

The problem is best explained by example, consider the following two models:

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

class Pizza(models.Model):
    name = models.CharField(max_length=100)
    toppings = models.ManyToManyField(Toppping)

My data looks like the following:

Pizza and Topping tables joined:

NAME        TOPPINGS
--------------------
deluxe      topping_1, topping_2
deluxe      topping_3, topping_4
hawaiian    topping_1

I want to get the toppings of all Pizza with name deluxe. Any ideas what kind of QuerySet I have to write to get that kind of result? The expected output for the above will be:

[topping_1, topping_2, topping_3, topping_4]
+4  A: 
Topping.objects.filter(pizza__name='deluxe')
Daniel Roseman