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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:26:51+00:00 2026-05-23T18:26:51+00:00

I have a page where users can see the comments list for a specific

  • 0

I have a page where users can see the comments list for a specific product.

In the view I get the list with:

comments = product.comments.all().order_by('-timestamp')

and this piece in the template:

1   {% if comments %}
2       {% for comment in comments %}
3       <div class="comment">
4           <a href="/user/{{ product.author }}/">{{ comment.author }}</a>
5           <br>{{ comment.text }}
6       </div>
7       {% endfor %}
8   {% endif %}

Using django debug toolbar when I go to a product page I can see that Django performs many queries are many comments in the list. For example in a product page with a comments list of 10 comments, Django performs about 15 queries. If I submit a new comment, the queries count rises to 16.

If i try to remove {{ comment.author }} (line 4), the queries decrease to a fixed number, if I submit new comments. How to optimize this?

NB: comment.author is a ForeignKey to a django.contrib.auth.models.User

  • 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-23T18:26:51+00:00Added an answer on May 23, 2026 at 6:26 pm

    I think this is a case for select_related?

    https://docs.djangoproject.com/en/dev/ref/models/querysets/#select-related

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

Sidebar

Related Questions

I have this web page where users can add smilies to their comments. And
Imagine that HTML page is a game surface (see picture). User can have n
I have a jqGrid on a page and users can click a button to
I have created a page on my site where users can post directly to
I have a subscription link on a page that the logged in users can
I have a page where a user can import data to the site. either
I have a page where the user can dynamically add file upload boxes. Adding
I have a page where the user can edit various content using buttons and
I have a page where a user can upload a file along with some
I have a page upon which a user can choose up to many different

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.