Skip to content

Commit 1f78c34

Browse files
authored
Merge pull request #60 from antoine-gallix/master
Improve docs on setup
2 parents 6e1da7d + 1b99dad commit 1f78c34

File tree

1 file changed

+55
-26
lines changed

1 file changed

+55
-26
lines changed

docs/initialize.rst

Lines changed: 55 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,57 @@ Create model with MPTT mixin:
2424
return "<Node (%s)>" % self.id
2525
2626
27-
It automatically registers events.
27+
28+
Session factory wrapper
29+
-----------------------
30+
31+
For the automatic tree maintainance triggered after session flush to work
32+
correctly, wrap the Session factory with :mod:`sqlalchemy_mptt.mptt_sessionmaker`
33+
34+
.. code-block:: python
35+
:linenos:
36+
37+
from sqlalchemy import create_engine
38+
from sqlalchemy.orm import sessionmaker
39+
from sqlalchemy_mptt import mptt_sessionmaker
40+
41+
engine = create_engine('...')
42+
Session = mptt_sessionmaker(sessionmaker(bind=engine))
43+
44+
Using session factory wrapper with flask_sqlalchemy
45+
---------------------------------------------------
46+
47+
If you use Flask and SQLAlchemy, you probably use also flask_sqlalchemy
48+
extension for integration. In that case the Session creation is not directly
49+
accessible. The following allows you to use the wrapper:
50+
51+
.. code-block:: python
52+
:linenos:
53+
54+
from sqlalchemy_mptt import mptt_sessionmaker
55+
from flask_sqlalchemy import SQLAlchemy
56+
57+
# instead of creating db object directly
58+
db = SQLAlchemy()
59+
60+
# subclass the db manager and insert the wrapper at session creation
61+
class CustomSQLAlchemy(SQLAlchemy):
62+
"""A custom SQLAlchemy manager, to have control on session creation"""
63+
64+
def create_session(self, options):
65+
"""Override the original session factory creation"""
66+
Session = super().create_session(options)
67+
# Use wrapper from sqlalchemy_mptt that manage tree tables
68+
return mptt_sessionmaker(Session)
69+
70+
# then
71+
db = CustomSQLAlchemy()
72+
2873
2974
Events
3075
------
3176

32-
But you can do it manually:
77+
The tree manager automatically registers events. But you can do it manually:
3378

3479
.. code-block:: python
3580
@@ -77,15 +122,15 @@ Represented data of tree like dict
77122
{'id': '10', 'parent_id': '7'},
78123
{'id': '11', 'parent_id': '10'},
79124
)
125+
Initializing a tree with data
126+
-----------------------------
80127

81-
Filling data at the first time
82-
------------------------------
83-
84-
When you add any data to the database, he tries to be counted lft,
85-
rgt and level attribute. This is done very quickly if the tree already
86-
exists in the database, but it is absolutely not allowed for initialize
87-
the tree, it is very long. In this case, you can change the code like
88-
this:
128+
When you add nodes to the table, the tree manager subsequently updates the
129+
level, left and right attributes in the reset of the table. This is done very
130+
quickly if the tree already exists in the database, but for initializing the
131+
tree, it might become a big overhead. In this case, it is recommended to
132+
deactivate automatic tree management, fill in the data, reactivate automatic
133+
tree management and finally call manually a rebuild of the tree once at the end:
89134

90135
.. no-code-block:: python
91136

@@ -109,19 +154,3 @@ this:
109154
models.MyModelTree.rebuild_tree(db.session, 'my_tree_1') # rebuild lft, rgt value automatically
110155

111156
After an initial table with tree you can use mptt features.
112-
113-
Session
114-
-------
115-
116-
To work correctly after flush you should use
117-
:mod:`sqlalchemy_mptt.mptt_sessionmaker`
118-
119-
.. code-block:: python
120-
:linenos:
121-
122-
from sqlalchemy import create_engine
123-
from sqlalchemy.orm import sessionmaker
124-
from sqlalchemy_mptt import mptt_sessionmaker
125-
126-
engine = create_engine('...')
127-
Session = mptt_sessionmaker(sessionmaker(bind=engine))

0 commit comments

Comments
 (0)