My favorites | Sign in
Project Home Downloads Wiki Issues Source
READ-ONLY: This project has been archived. For more information see this post.
Search
for
  Advanced search   Search tips   Subscriptions
Issue 26: Sorting field for tree
3 people starred this issue and may be notified of changes. Back to list
Status:  Fixed
Owner:  ----
Closed:  Nov 2010


 
Reported by mwdi...@gmail.com, Jul 28, 2008
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.
tree_order_patch.diff
10.7 KB   View   Download
Jul 28, 2008
#1 mwdi...@gmail.com
I should add that this patch was designed for Postgres, and probably will not work
for MySQL, as it makes use of the to_char() function. Oracle, however, should work.

The only thing that would need to be updated would be the SQL code in
managers.py/TreeOrder/_update_tree_order. The to_char(%(tree_id)s, '0000') constructs
need to become lpad(cast(%(tree_id)s as char), 4, '0') for MySQL. However, as I do
not use MySQL, I have no easy way to test that.
Dec 5, 2008
#2 mocksoul
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
#3 mocksoul
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
#4 mwdi...@gmail.com
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
#5 sergey.g...@gmail.com
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.
tree_order_patch.diff
10.0 KB   View   Download
to_char.sql
227 bytes   View   Download
Nov 13, 2010
Project Member #6 craig.ds@gmail.com
0.4 added MPTTModelAdmin and FeinCMSModelAdmin which take care of this in a nicer way than a db-specific patch
Status: Fixed

Powered by Google Project Hosting