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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:14:50+00:00 2026-05-17T16:14:50+00:00

I’m using django-threadedcomments and django-voting in my project to achieve a Reddit-like comments voting

  • 0

I’m using django-threadedcomments and django-voting in my project to achieve a Reddit-like comments voting system.

I’ve set everything up correctly and I’m able to successfully record votes for each threaded comment and its children, however I’m a bit stuck as to how to sort the comments so that the comment with the highest score rises to the top.

Would passing through a template tag be a solution? I’ve attempted this and returned a list where the items were ordered by score descending, however the parent-child relationship of the comments got messed up. Here is what I did:

class OrderByVotesNode(template.Node):
    def __init__(self, queryset_var, direction="desc"):
        self.queryset_var = template.Variable(queryset_var)
        self.direction = direction

    def render(self, context):
    key = self.queryset_var.var
    value = self.queryset_var.resolve(context)
    try:
        direction = template.Variable(self.direction).resolve(context)
    except template.VariableDoesNotExist:
        direction = "desc"
    model = value.model
    qn = connection.ops.quote_name
    ctype = ContentType.objects.get_for_model(model)
    by_score = model.objects.filter(id__in=[f.id for f in value]).extra(select={"score": """
            SELECT coalesce(SUM(vote), 0 )
            FROM %s
            WHERE content_type_id = %s
            AND object_id = %s.%s
        """ % (qn(Vote._meta.db_table), ctype.id, qn(model._meta.db_table), qn(model._meta.pk.attname))},
        order_by=[(direction == "desc" and "-" or "") + "score"])
    context[key] = by_score
    return u""

Any help or suggestions on this would be greatly appreciated. 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-17T16:14:51+00:00Added an answer on May 17, 2026 at 4:14 pm

    There are two strategies:

    1) You want to sort only on the topmost comment on every thread. Add a restriction on the query that this comment has no parent comment (I’m not sure how threadedcomments model is designed exactly).

    2) You take the ordered result, and traverse every comment to its root parent. Then, you filter out duplicates from the top to the bottom. This way, each thread is scored according to the maximum score of any comment in the thread. This is harder to implement, and frankly, doesn’t seem like the most intuitive order in my mind.

    After you’ve done this, you have an ordered list of comments that you display in a threaded fashion in the template (I assume you have some template recursion going on).

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

Sidebar

Related Questions

No related questions found

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.