The next query does what I'd want it to do, however, I've no clue whether it's efficient. I experienced the Django aggregation documentation, put it together, checked out the query and moved my mind sideways just like a confused dog.

Exactly what the query really does is get released Entry's "title" and "title_slug" which have a number of approved comments and orders the outcomes through the latest comment's "date_released" area. The outcomes are a listing of lately active Entry's.

So a couple of questions. (1) Can there be whatever you see within the query that's only a plain no-no. (2) It is possible to way will be able to begin to 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]

2) Yes, if configurations.DEBUG holds true, the raw sql queries are saved in django.db.connection.queries.

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

I have reduced the query for this:

active_entry_list = Entry.objects
    .values('name', 'name_slug')
    .filter(is_published=True, comment__approved='Y')
    .annotate(latest_comment=Max('comment__date_published'))
    .order_by('-latest_comment')
    [:6]

Whether it's blocking for comment__approved='Y' then you shouldn't have to count your comments ought to and filter them again with comments__gte=1.

I'll consider django.db.connection.queries. It seams simple. Thanks!