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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:38:18+00:00 2026-05-27T11:38:18+00:00

I need to get entries from database with counts of comments. Can i do

  • 0

I need to get entries from database with counts of comments. Can i do it with django’s comment framework? I am also using a voting application which is not using GenericForeignKeys i get entries with scores like this:

class EntryManager(models.ModelManager):
    def get_queryset(self):
        return super(EntryManager,self).get_queryset(self).all().annotate(\
            score=Sum("linkvote__value"))

But when there is foreignkeys i am being stuck. Do you have any ideas about that?

extra explaination: i need to fetch entries like this:

id | body | vote_score | comment_score |
 1 |  foo |         13 |             4 |
 2 |  bar |          4 |             1 |

after doing that, i can order them via comment_score. 🙂

Thans for all replies.

  • 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-27T11:38:19+00:00Added an answer on May 27, 2026 at 11:38 am

    Apparently, annotating with reverse generic relations (or extra filters, in general) is still an open ticket (see also the corresponding documentation). Until this is resolved, I would suggest using raw SQL in an extra query, like this:

    return super(EntryManager,self).get_queryset(self).all().annotate(\
        vote_score=Sum("linkvote__value")).extra(select={
            'comment_score': """SELECT COUNT(*) FROM comments_comment
                WHERE comments_comment.object_pk = yourapp_entry.id
                AND comments_comment.content_type = %s"""
        }, select_params=(entry_type,))
    

    Of course, you have to fill in the correct table names. Furthermore, entry_type is a “constant” that can be set outside your lookup function (see ContentTypeManager):

    from django.contrib.contenttypes.models import ContentType
    entry_type = ContentType.objects.get_for_model(Entry)
    

    This is assuming you have a single model Entry that you want to calculate your scores on. Otherwise, things would get slightly more complicated: you would need a sub-query to fetch the content type id for the type of each annotated object.

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

Sidebar

Related Questions

I'm using the following to get data from database in a php file: $query_pedVR
I'm using 3 AutocompleteTextViews to suggest entries from a database. I subclassed AutocompleteTextView to
In my Android app, I need to get 50,000 database entries (text) and compare
So I need to get hours, minutes and seconds out of entries like these:
Just need get some vals located in application.ini(main ini) in the Controller plugin I
I need to get the Handler to the child Window of a certain application
In one of my Django projects that use MySQL as the database, I need
I am new to MySQLdb. I need to read values from a pre-defined database
I need to display information from an SQL database which resides on a server,
I want to use this to get data from row out of mysql database

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.