| Issue 13: | optimize existing trees with mptt | |
| 11 people starred this issue and may be notified of changes. | Back to list |
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
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
Here is patch for rebuild tree functionality. Usage is simple: Model.tree.rebuild().
Dec 5, 2008
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
I tested this patch and it worked great with svn rev 121, great stuff! Thanks a ton! :)
Oct 21, 2009
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
Nov 18, 2009
Confirmed, this saved my ass a couple times where a 1k+ elements tree got corrupted, patch should definitely be accepted.
Dec 1, 2009
Very essential feature. Works like a charm.
Dec 2, 2009
Tested patch from comment #3, works fine. Thanks a lot! Hope to see this in the trunk.
Mar 15, 2010
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.
Mar 20, 2010
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
Upstream has gone away. Your patch is included here though: http://github.com/matthiask/django-mptt/commit/bdbdf0ad3edaa151aed39c9c89d0d43acdab17ad
Sep 2, 2010
(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
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 :). |
Owner: jonathan.buchanan
Labels: -Type-Defect Type-Enhancement