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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:59:43+00:00 2026-05-28T04:59:43+00:00

I am using django-threadedcomments , however the question also applies generally to sorting a

  • 0

I am using django-threadedcomments, however the question also applies generally to sorting a QuerySet.

The comment objects in the QuerySet have two important fields, tree_path and submit_date. tree_path is of the form “a/b/…/z” where ‘a’ is the highest-order index in the tree, and ‘b’ is the lowest order index in the tree. So the first root comment will have a tree_path of ‘1’. A child of that comment will have a tree_path of ‘1/1’. Another child of ‘1’ will have a tree_path of ‘1/2’. The second root comment will have a root_path of ‘2’, etc…

The QuerySet “qs” is sorted like above, with comments in threaded order with the oldest comments on top. Just the tree_paths of the above example would look like [1, 1/1, 1/2, 2]. I would like to sort each level of comments with the newest comments first. So the QuerySet instead should be [2, 1, 1/2, 1/1].

How can I do this?

I can sort just the root level comments by using:

qs = qs.extra(select={ 'tree_path_root': 'SUBSTRING(tree_path, 1, 1)' })
       .order_by('%stree_path_root' % ('-'), 'tree_path')

But I cannot figure out how to sort the non-root comments at the same time. I’ve tried something like:

qs = qs.extra(select={ 'tree_path_root': 'SUBSTRING(tree_path, 1, 1)' 
                       'tree_path_sec' : 'SUBSTRING(tree_path, 3, 1)'})
       .order_by('%stree_path_root' % ('-'), '%stree_path_sec' % ('-'), 'tree_path')

But that destroys the threading of the comments.

Any suggestions? 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-28T04:59:44+00:00Added an answer on May 28, 2026 at 4:59 am

    I realize this has been a little while since you posted.. so you may have an answer by now, or maybe you have moved on. Regardless, here you go… 🙂

    You are misunderstanding the tree_path structure in the django-threadedcomments application. There will never be a tree_path of 1/1, as each path segment is the unique primary key of that ThreadedComment.

    If you start with ThreadedComment 1, and add a reply, you will get a path of 1/2. Then if you add an additional top-level post, it would get the path 3. This would give you:

    1
    1/2
    3
    

    And if you reply to the first post again, you would get:

    1
    1/2
    1/4
    3
    

    Now to address the sorting issue. I have attempted to do a similar sorting (by a voting score, similar to reddit), and found no easy way to do it. However, here is a recursive method that you can use: (It is ugly, and slow… but its a starting point)

    def sort_comments(tree):
        final_tree = []
        root_comments = [c for c in tree if c.tree_path.count('/') == 0]
        root_comments.sort(key=lambda comment: comment.submit_date, reverse=True)
        for comment in root_comments:
            final_tree.append(comment)
            append_and_sort_children(final_tree, tree, comment)
        return final_tree
    
    
    def append_and_sort_children(final_tree, tree, parent):
        children = [c for c in tree if c.parent_id == parent.id]
        children.sort(key=lambda comment: comment.submit_date, reverse=True)
        for comment in children:
            final_tree.append(comment)
            append_and_sort_children(final_tree, tree, comment)
    

    Using this, simply pass in your entire query set of comments for that model, and python will sort them for you. 🙂

    This will give you the final result of:

    3
    1
    1/4
    1/2
    

    If anyone has a way to shorten this, feel free to contribute.

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

Sidebar

Related Questions

I am currently using Django to construct JSON encoded objects which a retrieved using
I just started using django for development. At the moment, I have the following
I using Django and a generic view django.views.generic.create_update.create_object I have a model form wich
I am using django-haystack for search. By default it is showing oldest objects first
I am using django-pagination to paginate the a list of objects in my temlate.
Using django 1.4 and have seen that when you use startproject it now creates
Using Django with a PostgreSQL (8.x) backend, I have a model where I need
When using Django's built in comment package, where is the table created when the
Im using Django 1.3.1.In my django code, i have the following template for the
I'm using django and jquery to implement authenticated sessions and Ajax requests. I have

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.