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

  • Home
  • SEARCH
  • 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 4571372
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T19:29:40+00:00 2026-05-21T19:29:40+00:00

New to Django, third question on it today… I have the following (simplified) models

  • 0

New to Django, third question on it today…

I have the following (simplified) models defined:

class Band(models.Model):
    bandname = models.CharField('Band name', max_length=300)

class Release(models.Model):
    title = models.CharField(max_length=300)
    artist = models.ManyToManyField('Band')
    formats = models.ManyToManyField('Format')

class Format(models.Model):
    kind = models.CharField(max_length=300)

class Song(models.Model):
    title = models.CharField(max_length=500)
    release = models.ForeignKey('Release')

(I’ve removed extra fields for the sake of readability)

Now, when I try to run the following query, it adds hundreds of extra queries to the page:

s = Song.objects.all() # adds 367 queries

I’ve tried changing it to this:

s = Song.objects.select_related('band', 'release').all() # adds 245 queries

This still sucks and I don’t know what else I can do. There are 122 songs in my database, on 52 releases, by 39 bands. Not sure if this helps but I’m at a loss.

Any tips on how to optimise this? I need to be able to show the band and release name for each Song. A Release is ManyToMany for Band because some of them are split releases by multiple artists.

thanks,
Matt

  • 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-21T19:29:41+00:00Added an answer on May 21, 2026 at 7:29 pm

    Django select_related does not follow many-to-many relationships, and I’m guessing that is what is causing the large query count.

    My suggestion is to use values(), and double__underscore__notation to get the required values. Something similar to this should work on django 1.3 and above:

    s = Song.objects.values('title', 'release__title', 'release__artist__bandname')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have trouble transforming the following code into the new django 1.3 class-based generic
I am relatively new to django. I have defined my db schema and validated
I have a new Django application I am working on. I have the following
I'm new to Django, but the application that I have in mind might end
I'm rather new to Django and I'm using Django 1.0. I have this: forms.py:
I'm new to Django but I seem to have nearly identical code working on
I'm new to Django. I am writing my own administrative action for a third
I'm new to python and django but wanted to start following some tutorials. I
I'm new to Django and have some code in my views.py like this: try:
I'm new to Django, trying to process some forms. I have this form for

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.