google-code-export / django-mptt

Automatically exported from code.google.com/p/django-mptt
Other
0 stars 0 forks source link

optimize existing trees with mptt #13

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
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.

Original issue reported on code.google.com by bros...@gmail.com on 6 Feb 2008 at 5:24

GoogleCodeExporter commented 9 years ago
This would definitely be useful - I have a few ideas on the FeaturePlanning page
about it:

http://code.google.com/p/django-mptt/wiki/FeaturePlanning

Original comment by jonathan.buchanan on 6 Feb 2008 at 7:57

GoogleCodeExporter commented 9 years ago
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

Original comment by bros...@gmail.com on 6 Feb 2008 at 11:56

GoogleCodeExporter commented 9 years ago
Here is patch for rebuild tree functionality. Usage is simple: 
Model.tree.rebuild().

Original comment by mocksoul on 5 Dec 2008 at 10:46

Attachments:

GoogleCodeExporter commented 9 years ago
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.

Original comment by mocksoul on 5 Dec 2008 at 10:50

GoogleCodeExporter commented 9 years ago
I tested this patch and it worked great with svn rev 121, great stuff! Thanks a 
ton! :)

Original comment by zera...@gmail.com on 14 Jul 2009 at 7:00

GoogleCodeExporter commented 9 years ago
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

Original comment by bluesk...@gmail.com on 21 Oct 2009 at 10:21

Attachments:

GoogleCodeExporter commented 9 years ago
Confirmed, this saved my ass a couple times where a 1k+ elements tree got 
corrupted, patch should definitely 
be accepted.

Original comment by mbonetti on 18 Nov 2009 at 3:02

GoogleCodeExporter commented 9 years ago
Very essential feature. Works like a charm. 

Original comment by litchfie...@gmail.com on 2 Dec 2009 at 6:34

GoogleCodeExporter commented 9 years ago
Tested patch from comment #3, works fine. Thanks a lot!
Hope to see this in the trunk.

Original comment by neithere on 2 Dec 2009 at 6:23

GoogleCodeExporter commented 9 years ago
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.

Original comment by alexclau...@gmail.com on 15 Mar 2010 at 10:41

Attachments:

GoogleCodeExporter commented 9 years ago
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?

Original comment by mocksoul on 21 Mar 2010 at 12:00

GoogleCodeExporter commented 9 years ago
Upstream has gone away. Your patch is included here though:

http://github.com/matthiask/django-mptt/commit/bdbdf0ad3edaa151aed39c9c89d0d43ac
dab17ad

Original comment by matthias...@gmail.com on 15 Jun 2010 at 3:24

GoogleCodeExporter commented 9 years ago
(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/330f67999f8a89a58674d509e7753a
ca8724f158 330f67999f] I removed the custom SQL and replaced it with an 
ORM-based method, which seems cleaner to me.

Any objections? 

Original comment by craig.ds@gmail.com on 2 Sep 2010 at 9:01

GoogleCodeExporter commented 9 years ago
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 :).

Original comment by mocksoul on 2 Sep 2010 at 12:47