For the following 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 is as follows:
Pizza tables and countertops are connected:
ID NAME TOPPINGS
------------------------------------
1 deluxe topping_1, topping_2
2 deluxe topping_3, topping_4
3 hawaiian topping_1
I want to get the pizza id along with my corresponding toppings for the whole pizza with the name deluxe. My expected result:
1 topping_1
1 topping_2
2 topping_3
2 topping_4
Connection table:
pizza_toppings
--------------
id
pizza_id
topping_id
Here is the SQL equivalent that I want to achieve:
SELECT p.id, t.name
FROM pizza_toppings AS pt
INNER JOIN pizza AS p ON p.id = pt.pizza_id
INNER JOIN topping AS t ON t.id = pt.topping_id
WHERE p.name = 'deluxe'
Any ideas on what the matching Jango Queryset looks like? I also want to sort the resulting fillings by name, if the above is not complicated enough.
source
share