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

  • Home
  • SEARCH
  • 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 4583832
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T21:16:11+00:00 2026-05-21T21:16:11+00:00

How do I filter queryset by the number of comments, and order by number

  • 0

How do I filter queryset by the number of comments, and order by number of comments descending?

I tried to do something like Post.objects.filter(comment_count > 0).order_by('-comment_count') but that didn’t work or course.

Thanks!

My Post model:

class Post(models.Model):
 nickname = models.CharField(max_length=200, default=u'anonymous')
 body = models.TextField()
 pub_date = models.DateTimeField('Date Published', auto_now_add=True)
 up_date = models.DateTimeField('Date Updated', auto_now=True)
 category = models.ForeignKey(Category, related_name='post_category')
 counter = models.IntegerField(default=0)
 status = models.IntegerField(choices=POST_STATUS, default=0)
 votes = models.IntegerField('Votes', default=0)

Edit:

Just added the following code

from django.contrib.contenttypes import generic
from django.contrib.comments.models import Comment

comments = generic.GenericRelation(Comment, object_id_field="object_pk")

And in my view:

post_list = Post.objects.annotate(comment_count=Count('comments')).filter(status=STATUS.ACCEPTED).filter(comment_count__gt=0).order_by('-comment_count')

I fixed my model and view code. They are now working fine.

Thanks!

  • 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-21T21:16:12+00:00Added an answer on May 21, 2026 at 9:16 pm

    Using ‘annotations’; something like this:

    from django.db.models import Count
    Post.objects.annotate(comment_count=Count('comments')).filter(comment_count__gt=0).order_by('-comment_count')
    

    See the following for more info:
    http://docs.djangoproject.com/en/dev/topics/db/aggregation/

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

Sidebar

Related Questions

How can I order by descending my query set in django by date? Reserved.objects.all().filter(client=client_id).order_by('check_in')
I have a list of objects from a django queryset, e.g. my_list = MyObject.objects.filter(variable=something)
I have this QuerySet: brand = get_object_or_404(Brand, shortcut=shortcut) ads = Ad.objects.filter(mod__submodel__model__brand = brand).values('id') But
When filtering a queryset, I'm wondering if the following are equivalent. User.objects.filter(username='josh').filter(email__startswith='josh') User.objects.filter(username='josh', email__startswith='josh')
Filtering QuerySets in Django work like the following: Entry.objects.filter(year=2006) How can I use filter
This line works and returns the value that I'm looking for: logs = Log.objects.filter(filterURI=aFilter.uri).values()[0]['yk']
I have a queryset of providers : >>> provider_qs = Provider.objects.filter(...) [<Provider: Gin Investors>,
I need order a Queryset by date in desc order, but i need put
I have such QuerySet in Django. j = piosenki.objects.select_related('projekt__postac').distinct().exclude(typ__in=[live, remix]).filter(piosenki__plyta__status=1, piosenki__plyta__typ='s') .extra(select={ 'ocena' :
I have the following in one of my forms: self.fields['advisor'].queryset = User.objects.filter(groups__name='advisor') The only

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.