Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 367021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T13:41:18+00:00 2026-05-12T13:41:18+00:00

For the following models: class Topping(models.Model): name = models.CharField(max_length=100) class Pizza(models.Model): name = models.CharField(max_length=100)

  • 0

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 looks like the following:

Pizza and Topping tables joined:

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 their corresponding toppings for all pizza named deluxe. My expected result is:

1   topping_1
1   topping_2
2   topping_3
2   topping_4

The junction table is:

pizza_toppings
--------------
id    
pizza_id    
topping_id

Here’s the SQL equivalent of what 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 corresponding Django Queryset looks like? I also want to sort the resulting toppings by name if the above is not challenging enough.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T13:41:18+00:00Added an answer on May 12, 2026 at 1:41 pm

    I don’t think there is a clean solution to this, since you want data from two different models. Depending on your data structure you might want to use select_related to avoid hitting the database for all the toppings. Going for your desired result, I would do:

    result = []
    pizzas = Pizza.objects.select_related().filter(name='deluxe')
    for pizza in pizzas:
        for toppings in pizza.toppings.all():
            result.append((pizza.pk, topping.name))
    

    This would generate:

    [
        (1, topping_1),
        (1, topping_2),
        (2, topping_3),
        (2, topping_4),
    ]
    

    Now there are different ways to setup the data, using lists, tuples and dictionaries, but I think you get the idea of how you could do it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following models: class City(models.Model): name = models.CharField(max_length=100) class Pizza(models.Model): name =
Consider the following models and form: class Pizza(models.Model): name = models.CharField(max_length=50) class Topping(models.Model): name
I have the following models class Person(models.Model): name = models.CharField(max_length=100) class Employee(Person): job =
I have the following models: class Topping(models.Model): ... class Pizza(models.Model): toppings = models.ManyToManyField(Topping) I
I have the following models: class Indicator(models.Model): name = models.CharField(max_length=200) category = models.ForeignKey(IndicatorCategory) weight
I have the following models: class Territory(models.Model): name = models.CharField(max_length=30) power = models.ForeignKey(Power, null=True,
Say I have the following models: class Baz(models.Model): winning = models.CharField(max_length=100) class Bar(models.Model): baz
I have following models class Artist(models.Model): name = models.CharField(max_length=200, unique=True) photo = models.CharField(max_length=250) views
I have the following models class Book(models.Model): name = models.CharField(max_length=140) class UserProfile(models.Model): favorites =
For the following models: class Entity(models.Model): name = models.CharField(max_length=256) class Entry(models.Model): A <subj> has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.