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 8714503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:36:44+00:00 2026-06-13T05:36:44+00:00

Consider a shema with a Publishers table and a Books table so that def

  • 0

Consider a shema with a Publishers table and a Books table so that

def Publisher(models.Model):
  city = models.CharField()
  ...

def Book(models.Model):
  title = models.CharField()
  publisher = models.ForeignKey(Publisher)

In my template I wish to display a list of certain publishers with all its books. In my view function I fetch the desired Publishers with something like

publishers=Publisher.objects.filter(city='NY')

Then in my view I iterate over publishers and publishers.book_set.all like

{% for p in publishers %}
....
   {% for b in p.book_set.all %}

This works as expected except that it obviously hits the db like a zillion times.

How can I optimise the code so that django only hits the db once or twice?

  • 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-06-13T05:36:45+00:00Added an answer on June 13, 2026 at 5:36 am

    In Django 1.4+, use prefetch_related

    Publisher.objects.filter(city='NY').prefetch_related('book_set')
    

    In Django <1.4, use django-batch-select.

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

Sidebar

Related Questions

Consider the model schema: class A(models.Model): id = models.IntegerField(...) ... class B(models.Model): parent =
Consider the following table with a constraint that's a bit daft but simple enough
Consider a table with the following schema: id, location, starred There are many records
Let's consider the following scenario: I've a master table with a detail table. The
Please consider following schema CREATE table articles ( id Int UNSIGNED NOT NULL AUTO_INCREMENT,
Consider a simple schema of one to many relationship. Parent Table's Id is referenced
Consider an entity, Entry, that contains a collection of another entity (many-to-many), Category, such
Consider the database schema in the picture. I need a linq-to-entities statement that will
Consider the following database schema: create table UserGroup ( id int not null auto_increment,
Symfony uses the schema.yml to create a module for every db table. So consider

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.