django-mptt: how to successfully move nodes around

Posted by Parand on Stack Overflow See other posts from Stack Overflow or by Parand
Published on 2010-06-14T19:21:47Z Indexed on 2010/06/15 12:42 UTC
Read the original article Hit count: 493

Filed under:
|
|

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?

© Stack Overflow or respective owner

Related posts about python

Related posts about django