Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django-mptt: how to successfully move nodes around

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?

like image 446
Parand Avatar asked Jun 14 '10 19:06

Parand


2 Answers

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.

like image 58
Dominic Rodger Avatar answered Oct 30 '22 19:10

Dominic Rodger


This library has really confused me these last few days -- move_to does not really seem to do what I want it to, and my tree keeps getting out of sync. I came up with a solution that I'm more confident in, at the expense of speed and nontraditional-ness.

It revolves around the manager method partial_rebuild here.

def delete_node(self):
    if not self.parent:
        print("Should not delete root node, confusing behavior follows")
        return
    tree_id = self.tree_id
    parent = self.parent

    for child in self.get_children():
        child.parent = parent
        child.save()

    self.delete()
    Node.objects.partial_rebuild(tree_id)

You can replace child.parent = parent with child.move_node(parent) if you'd like

like image 27
Sam Bobel Avatar answered Oct 30 '22 20:10

Sam Bobel