33
44Create model with MPTT mixin:
55
6- .. code-block :: python
7- :linenos:
6+ .. testcode ::
87
98 from sqlalchemy import Column, Integer, Boolean
109 from sqlalchemy.ext.declarative import declarative_base
@@ -31,14 +30,13 @@ Session factory wrapper
3130For the automatic tree maintainance triggered after session flush to work
3231correctly, wrap the Session factory with :mod: `sqlalchemy_mptt.mptt_sessionmaker `
3332
34- .. code-block :: python
35- :linenos:
33+ .. testcode ::
3634
3735 from sqlalchemy import create_engine
3836 from sqlalchemy.orm import sessionmaker
3937 from sqlalchemy_mptt import mptt_sessionmaker
4038
41- engine = create_engine(' ... ' )
39+ engine = create_engine('sqlite:/// :memory: ')
4240 Session = mptt_sessionmaker(sessionmaker(bind=engine))
4341
4442Using session factory wrapper with flask_sqlalchemy
@@ -48,8 +46,7 @@ If you use Flask and SQLAlchemy, you probably use also flask_sqlalchemy
4846extension for integration. In that case the Session creation is not directly
4947accessible. The following allows you to use the wrapper:
5048
51- .. code-block :: python
52- :linenos:
49+ .. testcode ::
5350
5451 from sqlalchemy_mptt import mptt_sessionmaker
5552 from flask_sqlalchemy import SQLAlchemy
7673
7774The tree manager automatically registers events. But you can do it manually:
7875
79- .. code-block :: python
76+ .. testcode ::
8077
8178 from sqlalchemy_mptt import tree_manager
8279
@@ -85,7 +82,7 @@ The tree manager automatically registers events. But you can do it manually:
8582
8683Or disable events if it required:
8784
88- .. code-block :: python
85+ .. testcode ::
8986
9087 from sqlalchemy_mptt import tree_manager
9188
@@ -103,7 +100,7 @@ Fill table with records, for example, as shown in the picture
103100
104101Represented data of tree like dict
105102
106- .. code-block :: python
103+ .. testcode ::
107104
108105 tree = (
109106 {'id': '1', 'parent_id': None},
@@ -132,7 +129,28 @@ tree, it might become a big overhead. In this case, it is recommended to
132129deactivate automatic tree management, fill in the data, reactivate automatic
133130tree management and finally call manually a rebuild of the tree once at the end:
134131
135- .. no-code-block :: python
132+ .. testcode ::
133+ :hide:
134+
135+ from flask import Flask
136+
137+ class MyModelTree(db.Model, BaseNestedSets):
138+ __tablename__ = "my_model_tree"
139+
140+ id = db.Column(db.Integer, primary_key=True)
141+ visible = db.Column(db.Boolean) # you custom field
142+
143+ def __repr__(self):
144+ return "<Node (%s)>" % self.id
145+
146+ app = Flask('test')
147+ app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory: '
148+ db.init_app(app)
149+ app.app_context().push()
150+ db.create_all()
151+ items = [MyModelTree(**data) for data in tree]
152+
153+ .. testcode ::
136154
137155 from sqlalchemy_mptt import tree_manager
138156
@@ -144,13 +162,13 @@ tree management and finally call manually a rebuild of the tree once at the end:
144162 for item in items:
145163 item.left = 0
146164 item.right = 0
147- item.tree_id = 'my_tree_1'
165+ item.tree_id = 1
148166 db.session.add(item)
149167 db.session.commit()
150168
151169 ...
152170
153171 tree_manager.register_events() # enabled MPTT events back
154- models. MyModelTree.rebuild_tree(db.session, 'my_tree_1' ) # rebuild lft, rgt value automatically
172+ MyModelTree.rebuild_tree(db.session, 1 ) # rebuild lft, rgt value automatically
155173
156174After an initial table with tree you can use mptt features.
0 commit comments