| Issue 26: | Sorting field for tree | |
| 3 people starred this issue and may be notified of changes. | Back to list |
One frustration which I have found in the Admin (newforms-admin as well) is that there is no way to sort on multiple columns. Such a sort is necessary if you want your models to appear in MPTT traversal order, in the change-list. For example, I have extended the change list template to integrate a column containing a variant of the form provided by django-mptt. This allows the nodes to be moved within the change list. I have also changed the first column in the change list to be indented by node level. However, there is no way to make the Admin sort, because to do so it would need to sort by [tree_id], [level], and [lft], in that order. This patch extends django-mptt, adding a tree_order column. This column is automatically maintained by the TreeManager. If you specify the tree_order column in the ordering clause of admin, the objects will then appear in tree order. A more permanent solution would be for contrib.admin to support multi-column sorting. However, for the time being, this would resolve the problem.
Jul 28, 2008
#1
mwdi...@gmail.com
Dec 5, 2008
Why sort by level? You need just sort by tree_id and left. Data will be in exact order as it should. E.g Node NodeX NodeY After that, NodeZ.insert_at(NodeX, 'right'), will make Node NodeX NodeZ NodeY So, if you order only by left - you will get rows in same order it defined. Btw, mptt ordering designed for this, if you want automatically order nodes by insertion.
Dec 5, 2008
Nice way to see RIGHT ordering:
from mptt.utils import tree_item_iterator
for x in tree_item_iterator(Model.objects.order_by('tree_id', 'lft')):
if x[1]['new_level']: level += 1
print ' ' * level, x[0]
level -= len(x[1]['closed_levels'])
Jul 9, 2009
Right. Level is not required. However, your solution is no solution at all if you want to use this order in the admin. The Admin only allows sorting by a single column. If you specify multiple columns, either in the Model, on in the ModelAdmin, it ignores all columns but the first one.
Nov 1, 2009
Hello! I've attached patch against latest revision. Level value is excluded from tree_order field value. Also quick replacement for to_char function atteched for mysql users.
Nov 13, 2010
0.4 added MPTTModelAdmin and FeinCMSModelAdmin which take care of this in a nicer way than a db-specific patch
Status:
Fixed
|