| Issue 25: | node.delete() messes up structure | |
| 1 person starred this issue and may be notified of changes. | Back to list |
I have a fairly large tree (about 5k entries), generated from an old simple
parent->child structured tree with the following code:
from mymodels import Chain
class State(object):
_tree_id = 0
_next = 0
lft_dupes = []
rght_dupes = []
real_lft_dupes = []
real_rght_dupes = []
def __init__(self):
self.lft_dupes = []
self.rght_dupes = []
self.real_lft_dupes = []
self.real_rght_dupes = []
def next(self):
self._next += 1
return self._next
next = property(next)
Chain.objects.all().update(lft=0, rght=0, level=0)
def has_children(node):
return node.children.count() > 0
def recurse(node, state):
if has_children(node):
for child in node.children.all().order_by('step', 'substep',
'item__label'):
child.tree_id = node.tree_id
child.level = node.level + 1
child.lft = state.next
child.save()
if has_children(child):
recurse(child, state)
child.rght = state.next
child.save()
node.rght = state.next
node.save()
if node.lft in state.lft_dupes:
state.real_lft_dupes.append(node.lft)
else:
state.lft_dupes.append(node.lft)
if node.rght in state.rght_dupes:
state.real_rght_dupes.append(node.rght)
else:
state.rght_dupes.append(node.rght)
print node.pk, node.lft, node.rght, node.tree_id, node.level
def regenerate(node, tree_id = None):
state = State()
if tree_id is not None:
node.tree_id = tree_id
node.level = 0
node.lft = state.next
node.save()
recurse(root, state)
print state.real_lft_dupes
print state.real_rght_dupes
node.rght = state.next
if __name__ == '__main__':
roots = Chain.get_first_level_nodes()
tree_id = 0
for root in roots:
regenerate(root, tree_id)
tree_id += 1
It will print out if there are any dupes in the lft, rght fields - there
are not.
When I take a subtree and delete() it, I end up with duplicated lft and
rght fields and need to rebuild the tree to avoid funny effects (like a
totally messed up display of the tree structure). I haven't seen any
obvious things in the pre_delete signal, so I don't really know what's
going on here ...
Jul 24, 2008
#1
lukasor...@gmail.com
Oct 12, 2008
(No comment was entered for this change.)
Status:
Duplicate
|