django-mptt는 나를 정신 박약니다. 나는 상대적으로 간단한 것을하려고 노력하고있다 : 나는 노드를 삭제할 것이고, 노드의 자식들과 합리적인 것을 할 필요가있다. 즉, 나는 그들을 한 수준 위로 움직여 그들이 현재 부모의 부모의 자녀가되도록하고 싶다. 내가 아버지를 삭제하는거야, 할아버지의 자녀로 C1 및 C2 싶습니다django-mptt : 노드를 성공적으로 이동하는 방법
Root
|
Grandpa
|
Father
| |
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 in grandpa.children.all() # True
들 C1 및 C2를 계산하지만, 루트 아이들을 부인하는.
c1.get_root() == father # c1's root is father, instead of Root
c1 in root.get_descendants() # False
어떻게 아이들을 이동시키고 그 루트가 손상되지 않습니까?
를 원한다면 당신은 child.move_node (부모)와 child.parent = 부모를 대체 할 수 있습니까? – mawimawi
이 경우 노드를 실제로 삭제하지 않고 보관합니다. 나는 나무에서 그것을 제거하고 싶습니다. 당신은 좋은 지적을 가지고 있습니다, 나는 실제로 나무에서 그것을 제거하지 않습니다. – Parand
부모를 None으로 설정하고 저장하는 것은 실제로 트리에서 노드를 제거하는 방법입니다 (mptt 테스트 사례에 따라). – Parand