django-mpttは、私を頭から追い出そうと決心しているようです。私は比較的単純なことをしようとしています。ノードを削除し、ノードの子に対して合理的なことをする必要があります。つまり、現在の親の子になるように、1つ上のレベルに移動したいと思います。
つまり、ツリーが次のようになっている場合:
Root
|
Grandpa
|
Father
| |
C1 C2
父を削除し、C1とC2をおじいちゃんの子にします。
これが私が使用しているコードです:
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()
だから私は呼ぶだろう:
father.reparent_children(grandpa)
father.parent = None
father.save()
これは機能します-ほとんど。子供たちは両親をおじいちゃんとして報告します:
c1.parent == grandpa # True
おじいちゃんはその子供たちの間でC1とC2を数えます
c1 in grandpa.children.all() # True
しかし、ルートはこれらの子供たちを否認します。
c1.get_root() == father # c1's root is father, instead of Root
c1 in root.get_descendants() # False
子供たちを動かして、彼らの根が壊れないようにするにはどうすればよいですか?