Issue 13: optimize existing trees with mptt
Status:  Fixed
Owner:
Closed:  Sep 2010
Project Member Reported by bros...@gmail.com, Feb 6, 2008
mptt works great when you are starting with a branch new tree. I have a couple of projects that 
have existing trees using the typical adjacency list model (only a parent field) to represent the tree. 
Uses django-mptt on top of those trees would be great.

I going to work on a fix for this, but wanted to mention it here. I am thinking a management 
command rebuild_tree that given a model in the form app.model it can dynamically attach itself or 
use the parameters given to mptt.register to rebuild the tree.
Feb 6, 2008
Project Member #1 jonathan.buchanan
This would definitely be useful - I have a few ideas on the FeaturePlanning page
about it:

https://code.google.com/p/django-mptt/wiki/FeaturePlanning
Status: Accepted
Owner: jonathan.buchanan
Labels: -Type-Defect Type-Enhancement
Feb 6, 2008
Project Member #2 bros...@gmail.com
Here is some VERY rough code for this process. It can be optimized in many ways. It can also be less hard-
coded ;)

def rebuild_tree(node, tree_id, lft=0, level=1):
    rght = lft + 1
    print "%s%s (%s)" % ("    " * (level - 1), node.name, unicode(node.parent))
    for child in node.children.all().order_by("name"):
        rght = rebuild_tree(child, tree_id, rght, level + 1)
    node.lft, node.rght = lft, rght
    node.tree_id = tree_id
    node.level = level
    node.save()
    return rght + 1
tree_id = 0
for root_node in Category.tree.root_nodes().order_by("name"):
    rebuild_tree(root_node, tree_id)
    tree_id += 1
Dec 5, 2008
#3 mocksoul
Here is patch for rebuild tree functionality. Usage is simple: Model.tree.rebuild().
rebuild_tree.patch
3.2 KB   View   Download
Dec 5, 2008
#4 mocksoul
Main advantage of my patch - it does NOT uses django ORM. In this case it can be 
easily used in database migrations (in my case - in `south` migrations), while ORM 
based way - cant. Thus, to migrate from adjacency list model to mptt tree model you 
just need to create appropriate columns in db and call rebuild() on TreeManager. 
That's all.

It even tries to utilize sorting, if any. I'll write unit tests for all 
functionality, if needed.
Jul 14, 2009
#5 zera...@gmail.com
I tested this patch and it worked great with svn rev 121, great stuff! Thanks a ton! :)
Oct 21, 2009
#6 bluesk...@gmail.com
the patch is good... I will just point out it is not Python 2.4 compatible though, there are two inline if statements 
that need rewriting in long form to make it work with the older version




rebuild_tree.patch
3.3 KB   View   Download
Nov 18, 2009
#7 mbonetti
Confirmed, this saved my ass a couple times where a 1k+ elements tree got corrupted, patch should definitely 
be accepted.
Dec 1, 2009
#8 litchfie...@gmail.com
Very essential feature. Works like a charm. 
Dec 2, 2009
#9 neithere
Tested patch from comment #3, works fine. Thanks a lot!
Hope to see this in the trunk.
Mar 15, 2010
#10 alexclau...@gmail.com
patch from comment 6 is slightly wrong. consider the case with no defined ordering...
it will still have 'ORDER BY ' at the end of the query, which is not the case with
the original patch.
rebuild_tree.patch
3.3 KB   View   Download
Mar 20, 2010
#11 mocksoul
Well. I'm using this patch for 1.5 years as of now, each time updating upstream and 
it still works without changes.

Hey, upstream, maybe include this stuff into main tree? What are you waiting for?
Jun 15, 2010
#12 matthias...@gmail.com
Upstream has gone away. Your patch is included here though:

http://github.com/matthiask/django-mptt/commit/bdbdf0ad3edaa151aed39c9c89d0d43acdab17ad
Sep 2, 2010
Project Member #13 craig.ds@gmail.com
(Upstream is back with a new face...)

Good patch, I've been using it for a while too.

In [http://github.com/django-mptt/django-mptt/commit/330f67999f8a89a58674d509e7753aca8724f158 330f67999f] I removed the custom SQL and replaced it with an ORM-based method, which seems cleaner to me.

Any objections? 
Status: Fixed
Owner: craig.ds
Sep 2, 2010
#14 mocksoul
In the days I wrote original patch where was no way to use ORM for constructing queries in mptt without directly importing application's models.

If now that functionality is available - obviously removing custom sql is preferrable solution. Thanks :).