Is moving/reordering root nodes possible?

350 views
Skip to first unread message

James Beith

unread,
Jul 2, 2015, 5:53:29 AM7/2/15
to django-...@googlegroups.com
If I had a tree structure consisting of three root nodes:

- Red
    - Fire Red
- Blue
    - Sea Blue
- Green
    - Forest Green

Is there a way to move/reorder the root nodes so I end up with this:

- Blue
    - Sea Blue
- Red
    - Fire Red
- Green
    - Forest Green

Thanks.

Sergio Garcia

unread,
Jul 2, 2015, 7:39:27 AM7/2/15
to django-...@googlegroups.com



--
You received this message because you are subscribed to the Google Groups "django-mptt-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-mptt-d...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

James Beith

unread,
Jul 2, 2015, 8:21:17 AM7/2/15
to django-...@googlegroups.com
The docs say:

A target of None indicates that the model instance should be turned into a root node. The position argument is disregarded in this case.

If the position argument is disregarded, how do you set the position of the root node?

Richard Dahl

unread,
Jul 2, 2015, 9:24:36 AM7/2/15
to django-...@googlegroups.com
There is only ONE root node per tree, what you have described is actually three trees, so if you want to change the order you need to change the order of the tree_id field in the django model.  
-richard

James Beith

unread,
Jul 3, 2015, 5:54:14 AM7/3/15
to django-...@googlegroups.com
Thank you both.

I guess I could either move to putting everything under one tree and then I'd have support to use the position argument with the move_to method.

- Colors
    - Red
        - Fire Red
    - Blue
        - Sea Blue
    - Green
        - Forest Green

Or if I wanted to keep the existing structure of multiple trees I could add an integer field to my model named order and then set this as the order_insertion_by class meta attribute. Then to reorder each of the trees' root nodes I change these values to reflect the order I want and then call rebuild() on the model manager to have the tree_id fields correctly updated to reflect the desired order. Downside to this approach is the docs say that the order_insertion_by should never be NULL and that it also affects the order of all nodes, not just root nodes. So I'd have to always manage this value for all nodes and I'd effectively be moving away from the API that django-mptt provides to do the ordering and instead just use this order value and rebuild() method.

I think the first approach sounds better. I can always test for the root node using is_root_node() and not display it when listing the nodes in a template for example.

Any more thoughts you guys have on this would be appreciated. Cheers.

Alex Kovrygin

unread,
Sep 10, 2018, 9:06:28 AM9/10/18
to django-mptt-dev
When having the same issue recently ended up in this topic, hope this will help someone in the future.

django-mptt supports moving trees, you just need to pass move_to() a neighbour and a relative position as follows:
red.move_to(blue, "right")

This will re-arrange tree_id values and thus update the order.

Apex World Cup

unread,
Oct 13, 2019, 5:33:12 PM10/13/19
to django-mptt-dev
ok
Reply all
Reply to author
Forward
0 new messages