Skip to content

Commit

Permalink
fix rebuild tree
Browse files Browse the repository at this point in the history
  • Loading branch information
uralbash committed Jun 22, 2018
1 parent f89efee commit dbe86fe
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
5 changes: 4 additions & 1 deletion docs/initialize.rst
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,16 @@ this:

# Fill tree
for item in items:
item.left = 0
item.right = 0
item.tree_id = 'my_tree_1'
db.session.add(item)
db.session.commit()

...

tree_manager.register_events() # enabled MPTT events back
models.MyModelTree.rebuild_tree(db.session, models.MyModelTree.tree_id) # rebuild lft, rgt value automatically
models.MyModelTree.rebuild_tree(db.session, 'my_tree_1') # rebuild lft, rgt value automatically

After an initial table with tree you can use mptt features.

Expand Down
16 changes: 14 additions & 2 deletions sqlalchemy_mptt/tests/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,21 @@ def test_documented_initial_insert(self):

tree_manager.register_events(remove=True) # Disable MPTT events

_tree_id = 'tree1'

for node_id, parent_id in [(1, None), (2, 1), (3, 1), (4, 2)]:
session.add(Tree(id=node_id, parent_id=parent_id))
item = Tree(
id=node_id,
parent_id=parent_id,
left=0,
right=0,
tree_id=_tree_id
)
session.add(item)
session.commit()

tree_manager.register_events() # enabled MPTT events back
Tree.rebuild_tree(session) # rebuild lft, rgt value automatically
Tree.rebuild_tree(
session,
_tree_id
) # rebuild lft, rgt value automatically

0 comments on commit dbe86fe

Please sign in to comment.