views:

134

answers:

1

django-mptt seems determined to drive me out of my mind. I'm trying to do something relatively simple: I'm going to delete a node, and need to do something reasonable with the node's children. Namely, I'd like to move them up one level so they're children of their current parent's parent.

That is, if the tree looks like:

 Root
  |
Grandpa
  |
Father
|    |
C1   C2

I'm going to delete Father, and would like C1 and C2 to be children of Grandpa.

Here's the code I'm using:

class Node(models.Model):
    first_name   = models.CharField(max_length=80, blank=True)
    parent       = models.ForeignKey('self', null=True, blank=True, related_name='children')

    def reparent_children(self, parent):
        print "Reparenting"
        for child in self.get_children():
            print "Working on", child.first_name, "to parent", parent.email
            parent = Node.objects.get(id=parent.id)
            child.move_to(parent, 'last-child')
            child.save()

So I'd call:

father.reparent_children(grandpa)
father.parent = None
father.save()

This works - almost. The children report their parents as Grandpa:

c1.parent == grandpa  # True

Grandpa counts C1 and C2 among its children

c1 in grandpa.children.all()   # True

However, Root disowns these kids.

c1.get_root() == father  # c1's root is father, instead of Root

c1 in root.get_descendants()  # False

How do I get the children to move and their root not get corrupted?

+1  A: 

The internal lft and rght values will change the first time you save a child (i.e. the final line of your reparent_children method. save() doesn't update instances you may have lying around. I think a safe way to do this would be to refetch them from the database each time, like this:

def reparent_children(self, parent):
    print "Reparenting"
    for child in self.get_children():
        print "Working on", child.first_name, "to parent", parent.email
        parent = Node.objects.get(id=parent.id)
        current_child = Node.objects.get(id = child.id)
        current_child.move_to(parent, 'last-child')
        current_child.save()

I had similar problems a while back, and that approach solved my problem.

Dominic Rodger
Dominic, I ended up at this method, and it *seems* to work, although with django-mptt I end up constantly questioning my own sanity. I don't know if I've actually fixed the issue or hidden it away somewhere else.
Parand