2

I'm trying to make a copy of a FeinCMS page tree, which is managed using django-mptt. I wrote this function:

def make_tree_copy(page, parent=None):
    '''
    Makes a copy of the tree starting at "page", reparenting it to "parent"
    '''
    new_page = Page.objects.create_copy(page)
    new_page.save()
    Page.tree.move_node(new_page, parent)

    # re-read so django-mptt fields get updated
    new_page = Page.objects.get(id=new_page.id)
    for child in page.get_children():
        # re-read so django-mptt fields get updated
        child = Page.objects.get(id=child.id)
        make_tree_copy(child, new_page)

and call it using

make_tree_copy(Page.tree.root_nodes()[0])

It works in general but when I have a page tree looking like this:

A
|- B
   |- C
   |- D

It comes out as this:

A
|- B
   |- D
   |- C

From my stepping through the mptt code, the magic seems to happen in mptt/managers.py/_inter_tree_move_and_close_gap(), where for some reason the "lft" values of the grandchildren get changed. Before the move they are C=3, D=5, afterwards they are C=5, D=3.

Which explains why D gets sorted before C but I have no idea why these values get switched. Any thoughts?

Paolo
  • 20,112
  • 21
  • 72
  • 113
Jonas
  • 970
  • 1
  • 8
  • 17

1 Answers1

4

Ok, I knew once I ask - I'd find the answer myself (after spending hours before...) Of course it's the same problem as in all the other django-mptt problems on StackOverflow: you have to re-read the object from the database.

I did so in the snippet above but at the wrong places. This is the code that works (re-reading the parent on entering the recursive function):

def make_tree_copy(page, parent=None):
    '''
    Makes a copy of the tree starting at "page", reparenting it to "parent"
    '''
    if parent:
        # re-read so django-mptt fields get 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)

    for child in page.get_children():
        make_tree_copy(child, new_page)
Jonas
  • 970
  • 1
  • 8
  • 17
  • Recent django-mptt versions are a bit smarter when it comes to updating the MPTT properties on model instances (lft, rght, level, etc.); maybe they work without reloading models over and over, I haven't tested it too much yet, though. – Matthias Kestenholz Feb 26 '11 at 12:35