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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:45:33+00:00 2026-05-15T03:45:33+00:00

I am using Django’s comment framework which utilizes generic foreign keys. Question: How do

  • 0

I am using Django’s comment framework which utilizes generic foreign keys.

Question: How do I sort a given model’s queryset by their comment count using the generic foreign key lookup?

Reading the django docs on the subject it says one needs to calculate them not using the aggregation API:

Django’s database aggregation API doesn’t work with a GenericRelation. […] For now, if you need aggregates on generic relations, you’ll need to calculate them without using the aggregation API.

The only way I can think of, though, would be to iterate through my queryset, generate a list with content_type and object_id‘s for each item, then run a second queryset on the Comment model filtering by this list of content_type and object_id … sort the objects by the count, then re-create a new queryset in this order by pulling the content_object for each comment …

This just seems wrong and I’m not even sure how to pull it off.

Ideas? Someone must have done this before.

I found this post online but it requires me handwriting SQL — is that really necessary ?

  • 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-15T03:45:33+00:00Added an answer on May 15, 2026 at 3:45 am

    The way you found in the blog post linked in your question is the way I’d do it (and, indeed, pretty much the way I did it in one of my own projects earlier this week)

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

Sidebar

Related Questions

I using Django and a generic view django.views.generic.create_update.create_object I have a model form wich
Using django, which is the way to check if data is present? I know
Using django comments framework http://docs.djangoproject.com/en/dev/ref/contrib/comments/ Not sure is there option, to make all comments
using django 1.4 I have a model with a datetimefield. I imported django.utils.timezone to
Using Django 1.1.1 In models.py: class Site(models.Model): name = models.CharField(max_length=50) class SiteMonth(models.Model): site =
When using django with apache which is the best server config? Should I use
I am using django-nonrel on the google-app-engine. When I'm posting a foreign character, in
I am using django-threadedcomments , however the question also applies generally to sorting a
I am using django-userena . I have a model called UserProfile . I've added
Using Django with a PostgreSQL (8.x) backend, I have a model where I need

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.