On Jan 20, 11:53 am, tlow <patu...@googlemail.com> wrote:
> I think you can not do that with only one query using annotate.
>
> You could do it manually in python using the code above including the
> additional filter on comment_approved and merge all (entry, number)
> pairs with Entry.objects.all() using 0 for entries which are not
> listed in your first query. Since you won't display thousands of
> entries at the same time, this should not be a problem.
>
> You could also use extra to insert a subquery into your sql statement.
> See.:http://docs.djangoproject.com/en/dev/ref/models/querysets/
>
> For example: (just the idea)
> entries = Entry.objects.all().extra(select={"approved_comment_count":
> "SELECT Count(*) FROM Comment WHERE Comment.Entry_id = Entry.id AND
> Comment.is_published = True AND Comment.approved = 'Y'"})
>
> Cheers,
> Thomas

Yep that query would be:

latest_entry_list = Entry.objects
         .filter(comment__approved='y')
        .annotate(comments=Count('comment'))
        .filter(is_published=True)
        .order_by('-date_published')
        [:15]

since you only want the count of approved comments
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to