performance - Django MPTT - how expensive is objects.rebuild? -


i roll out in next days application using django mptt manage hierarchical data. mptt provides function called rebuild, rebuilds trees available given model, , called such treenodes.objects.rebuild(). can see command called on model, not on instance of model. command has called after node has been inserted tree.

for django mptt 0.6 (not yet officially released) partial_rebuild command implemented, rebuild given tree.

while testing locally 10 trees no performance issue @ all, i'm concerned when there 100s of trees in db, , i'm calling rebuild command (which rebuild 100s of trees), might significant performance issue.

anybody out there has experience using rebuild command?

for future reference..

objects.rebuild() needed in special cases. mptt sets left , right values of nodes correctly setting parent id of node. mentioned in docs

i experienced issue not correctly set left, right values because saved new node, !before! reset position values of other, existing siblings. when inserting new nodes tree meta attribute order_insertion_by set, 1 has first reset order_insertion_by value siblings, , after that, save new node. way mptt able recalculate correctly left right values.

see (simplified) example below:

models.py

class node(mpttmodel):     """     representation of single node     """     name = models.charfield(max_length=200)     parent = treeforeignkey('self', null=true, blank=true, related_name='%(app_label)s_%(class)s_children')     position = models.positiveintegerfield(max_length=10) #for nodes on same hierarchy level have define position in displayed       class mpttmeta:         order_insertion_by = ['position'] 

views.py

new_node = node(name="new node", parent=parent_node, position=1) update_node_positions(new_node, 1) #routine update node positions new_node.save() 

update_node_positions

def update_node_positions(node, mode):     """     procedure update node positions     3 different modes available:         1 = insert node         2 = update position, parent stays same         3 = update position , change parent         4 = trashed     """      if mode == 1 or mode==3:         #if node has been inserted @ beginning         if node.position == 1:             node.get_siblings().update(position=f('position') + 1)         #if node has been inserted not @ beginning , not @ last position         elif node.position != node.get_siblings().count() + 1:             #update positions of siblings right of node 1             node.get_siblings().filter(position__gte=node.position).update(position=f('position') + 1)         if mode == 3:             #since removed node parent, have decrement positions of former siblings right of node 1             if node._original_parent not none:                 #do updates nodes had parent before. not executed root nodes                 node._original_parent.get_children().filter(position__gt=node._original_position).update(position=f('position') - 1)     if mode == 2:         #if old position left of new position -> decrement position 1 nodes have position <= node.position , > node.original_position         if node.position > node._original_position:             node.get_siblings().filter(q(position__lte=node.position) & q(position__gt=node._original_position)).update(position=f('position') - 1)         #if old position right of new position -> increment position 1 nodes have position >= node.position , < node.original_position          if node.position < node._original_position:             node.get_siblings().filter(q(position__gte=node.position) & q(position__lt=node._original_position)).update(position=f('position') + 1)     if mode == 4:         #decrement position 1 nodes have position > node.position         node.get_siblings().filter(q(position__gt=node.position)).update(position=f('position') - 1) 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -