django-mptt似乎下定决心要把我逼疯。我正在尝试做一些相对简单的事情:我要删除一个节点,并且需要对该节点的子节点做一些合理的处理。也就是说,我想要将它们提升一个级别,这样它们就是其当前父代的父代的子代。
也就是说,如果树看起来像:
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然而,Root与这些孩子断绝了关系。
c1.get_root() == father # c1's root is father, instead of Root
c1 in root.get_descendants() # False怎样才能让孩子们移动起来,而他们的根不会被破坏呢?
发布于 2010-06-15 20:41:01
第一次保存子对象(即reparent_children方法的最后一行)时,内部的lft和rght值将会更改。save()不会更新你可能闲置的实例。我认为一种安全的方法是每次都从数据库中重新获取它们,如下所示:
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()我曾经使用过similar problems,这种方法解决了我的问题。
发布于 2017-10-07 03:31:00
最近几天,这个库真的把我搞糊涂了-- move_to似乎并不能真正实现我想要的功能,而且我的树一直不同步。我想出了一个更有信心的解决方案,以牺牲速度和非传统性为代价。
它围绕着管理器方法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)如果您愿意,可以将child.parent = parent替换为child.move_node(parent)
https://stackoverflow.com/questions/3040214
复制相似问题