How to sort the hierarchy with tree_id desc?

693 views
Skip to first unread message

rmenon

unread,
Jan 5, 2012, 1:43:35 PM1/5/12
to django-mptt-dev
I am trying to create a threaded "commenting" system using django-
mptt .

If we assume that this is for a blog post, then for each blog post, I
want the following to happen

1. Each new comment on the blog post itself is shown first
2. Within each comment thread, the data is sorted based on creation
ts.


The generated query right now is:

SELECT `comment`.`id`, `comment`.`thread_id`, `comment`.`created_by`,
`comment`.`created_by_name`, `comment`.`comment_text`,
`comment`.`creation_ts`, `comment`.`parent_id`, `comment`.`lft`,
`comment`.`rght`, `comment`.`tree_id`, `comment`.`level` FROM
`comment` WHERE `comment`.`thread_id` = 1 ORDER BY `comment`.`tree_id`
ASC, `comment`.`lft` ASC

How do I change it so that we sort by "tree_id" DESC instead of
tree_id ASC which should take care of my requirements?

rmenon

unread,
Jan 5, 2012, 2:36:02 PM1/5/12
to django-mptt-dev
What I wanted works if I hard code the following change in the mptt
templatetags file in the method cache_tree_children. IF this
can be configured, then it would be great

Change line
>queryset = queryset.order_by(tree_id_attr, left_attr)
to
>queryset = queryset.order_by('-'+tree_id_attr, left_attr)

Craig de Stigter

unread,
Jan 5, 2012, 3:01:48 PM1/5/12
to django-...@googlegroups.com
Hi

The recursetree tag forces the ordering, which makes this kind of thing hard.

1. I think you can do this with iterative tags ( http://django-mptt.github.com/django-mptt/templates.html#examples ). They won't reorder anything, so you can give them a differently ordered queryset if you want.

2. If you *always* are rendering things in ('-tree_id', 'lft') order, consider changing order_insertion_by so your nodes are inserted in the correct order.

3. I've thought about making {{ children }} a tag, which it should probably have been originally. Then it could accept parameters like {% children reversed %} etc. Here's a ticket. Pull requests welcome :)

Craig

rmenon

unread,
Jan 5, 2012, 3:09:42 PM1/5/12
to django-mptt-dev
Thanx Craig! Will look into the various alternatives as soon as I
can.

Best

On Jan 5, 12:01 pm, Craig de Stigter <craig...@gmail.com> wrote:
> Hi
>
> The recursetree tag forces the ordering, which makes this kind of thing
> hard.
>
> 1. I think you can do this with iterative tags (http://django-mptt.github.com/django-mptt/templates.html#examples). They
> won't reorder anything, so you can give them a differently ordered queryset
> if you want.
>
> 2. If you *always* are rendering things in ('-tree_id', 'lft') order,
> consider changing order_insertion_by so your nodes are inserted in the
> correct order.
>
> 3. I've thought about making {{ children }} a tag, which it should probably
> have been originally. Then it could accept parameters like {% children
> reversed %} etc. Here's a
> ticket<https://github.com/django-mptt/django-mptt/issues/167>.

Alex S

unread,
Feb 6, 2012, 10:59:53 AM2/6/12
to django-...@googlegroups.com
Hello,
Regarding your second suggestion, are you sure that it's currently possible to specify a descending order in order_insertion_by ?

order_insertion_by = ('-created_at')

The above wont work because django-mptt will take '-created_at' as the field name instead of understanding a DESC order.


Craig de Stigter

unread,
Feb 6, 2012, 11:53:04 AM2/6/12
to django-...@googlegroups.com
You're right, my apologies. It probably should accept a desc order though, so I just made a ticket to add that https://github.com/django-mptt/django-mptt/issues/170

Craig

floriane brin

unread,
Jul 2, 2013, 3:50:46 PM7/2/13
to django-...@googlegroups.com
did you find a solution ?
 I'm trying to use order_insertion_by = ['-creation_date'] but I get the error :
Comment has no field named '-creation_date'

Thank you
Reply all
Reply to author
Forward
0 new messages