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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:32:42+00:00 2026-05-11T02:32:42+00:00

The following query does what I’d like it to do, however, I have no

  • 0

The following query does what I’d like it to do, however, I have no idea if it’s efficient. I went through the Django aggregation documentation, threw it together, looked at the query and tilted my head sideways like a confused dog.

What the query actually does is get published Entry’s ‘name’ and ‘name_slug’ that have one or more approved comments and orders the results by the latest comment’s ‘date_published’ field. The results are a list of recently active Entry’s.

So a few questions. (1) Is there anything you see in the query that’s just a plain no-no. (2) Is there a way that I can see the RAW SQL that’s querying the database?

Models:

class Entry(models.Model):     name = models.CharField(max_length=200, unique=True)     name_slug = models.SlugField(max_length=200, blank=True, editable=False, unique=True)     date_published = models.DateTimeField()     is_published = models.BooleanField(default=False)  class Comment(models.Model):     entry = models.ForeignKey('Entry')     date_published = models.DateTimeField()     approved_choices = (('N', 'No'), ('Y', 'Yes'), ('M', 'Needs Moderation'),)     approved = models.CharField(max_length=1, choices=approved_choices, default='N') 

Query:

active_entry_list = Entry.objects     .values('name', 'name_slug')     .filter(is_published=True, comment__approved='Y')     .annotate(latest_comment=Max('comment__date_published'),comments=Count('comment'))     .filter(comments__gte=1)     .order_by('-latest_comment')     [:6] 
  • 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. 2026-05-11T02:32:42+00:00Added an answer on May 11, 2026 at 2:32 am

    2) Yes, if settings.DEBUG is true, the raw sql queries are stored in django.db.connection.queries.

    http://blog.michaeltrier.com/2007/8/11/display-the-sql-django-orm-is-generating

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

Sidebar

Related Questions

Why does the following query raise the error below for a row with a
Does anyone know why the following query fails in Derby? delete from MyTable where
Can anyone explain what the following PHP Code does function query($query_string) { if ($query_string
I am wondering why the following regex does not match. string query = \1
The following query does not update the datetime field: update table SET EndDate =
Why does the following query work, when there is no column named Agentid in
The following query does not work, and produces an error: A transport-level error has
The following query does not return any result SELECT user_id FROM test WHERE poll_id
Following query does not work in access. SELECT Fields.FieldId, PrecisionSettings.DecimalPlaces from Fields left outer
The following query does not fail but does not insert the unique values either.

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.