Making a copy of a FeinCMS page tree using django-mptt changes child order -
i'm trying make copy of feincms page tree, managed using django-mptt. wrote function:
def make_tree_copy(page, parent=none): ''' makes copy of tree starting @ "page", reparenting "parent" ''' new_page = page.objects.create_copy(page) new_page.save() page.tree.move_node(new_page, parent) # re-read django-mptt fields updated new_page = page.objects.get(id=new_page.id) child in page.get_children(): # re-read django-mptt fields updated child = page.objects.get(id=child.id) make_tree_copy(child, new_page)
and call using
make_tree_copy(page.tree.root_nodes()[0])
it works in general when have page tree looking this:
a |- b |- c |- d
it comes out this:
a |- b |- d |- c
from stepping through mptt code, magic seems happen in mptt/managers.py/_inter_tree_move_and_close_gap(), reason "lft" values of grandchildren changed. before move c=3, d=5, afterwards c=5, d=3.
which explains why d gets sorted before c have no idea why these values switched. thoughts?
ok, knew once ask - i'd find answer myself (after spending hours before...) of course it's same problem in other django-mptt problems on stackoverflow: have re-read object database.
i did in snippet above @ wrong places. code works (re-reading parent on entering recursive function):
def make_tree_copy(page, parent=none): ''' makes copy of tree starting @ "page", reparenting "parent" ''' if parent: # re-read django-mptt fields updated parent = page.objects.get(id=parent.id) new_page = page.objects.create_copy(page) new_page.save() page.tree.move_node(new_page, parent) child in page.get_children(): make_tree_copy(child, new_page)
Comments
Post a Comment