sqlalchemy_mptt
package¶
Events¶
Base events¶
SQLAlchemy events extension
-
sqlalchemy_mptt.events.
mptt_before_insert
(mapper, connection, instance)[source]¶ Based on example https://bitbucket.org/zzzeek/sqlalchemy/src/73095b353124/examples/nested_sets/nested_sets.py?at=master
-
sqlalchemy_mptt.events.
mptt_before_update
(mapper, connection, instance)[source]¶ Based on this example: http://stackoverflow.com/questions/889527/move-node-in-nested-set
Mixins¶
SQLAlchemy nested sets mixin
-
class
sqlalchemy_mptt.mixins.
BaseNestedSets
[source]¶ Base mixin for MPTT model.
Example:
from sqlalchemy import Boolean, Column, create_engine, Integer from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker from sqlalchemy_mptt.mixins import BaseNestedSets Base = declarative_base() class Tree(Base, BaseNestedSets): __tablename__ = "tree" id = Column(Integer, primary_key=True) visible = Column(Boolean) def __repr__(self): return "<Node (%s)>" % self.id
-
tree_id
()¶ Represents a column in a database table.
-
parent_id
¶
-
parent
¶
-
left
()¶ Represents a column in a database table.
-
right
()¶ Represents a column in a database table.
-
level
()¶ Represents a column in a database table.
-
drilldown_tree
(session=None, json=False, json_fields=None)[source]¶ This method generate a branch from a tree, begining with current node.
For example:
node7.drilldown_tree()
level Nested sets example 1 1(1)22 --------------------- _______________|_________|_________ | | | | | | 2 2(2)5 6(4)11 | 12(7)21 | | ^ | ^ | 3 3(3)4 7(5)8 9(6)10 | 13(8)16 17(10)20 | | | | | 4 | 14(9)15 18(11)19 | | | ---------------------
Example in tests:
sqlalchemy_mptt.tests.cases.get_tree.test_drilldown_tree
-
get_children
(session=None)[source]¶ https://github.com/uralbash/sqlalchemy_mptt/issues/64 https://github.com/django-mptt/django-mptt/blob/fd76a816e05feb5fb0fc23126d33e514460a0ead/mptt/models.py#L563
Returns a query containing the immediate children of this model instance, in tree order.
For example:
node7.get_children() -> [Node(8), Node(10)]
level Nested sets example 1 1(1)22 ______________|____________________ | | | | | | 2 2(2)5 6(4)11 12(7)21 | ^ / 3 3(3)4 7(5)8 9(6)10 / 13(8)16 17(10)20 | | 4 14(9)15 18(11)19
-
classmethod
get_default_level
()[source]¶ Compatibility with Django MPTT: level value for root node. See https://github.com/uralbash/sqlalchemy_mptt/issues/56
-
get_siblings
(include_self=False, session=None)[source]¶ https://github.com/uralbash/sqlalchemy_mptt/issues/64 https://django-mptt.readthedocs.io/en/latest/models.html#get-siblings-include-self-false
Creates a query containing siblings of this model instance. Root nodes are considered to be siblings of other root nodes.
For example:
node10.get_siblings() -> [Node(8)]
Only one node is sibling of node10
level Nested sets example 1 1(1)22 ______________|____________________ | | | | | | 2 2(2)5 6(4)11 12(7)21 | ^ / 3 3(3)4 7(5)8 9(6)10 / 13(8)16 17(10)20 | | 4 14(9)15 18(11)19
-
classmethod
get_tree
(session=None, json=False, json_fields=None, query=None)[source]¶ This method generate tree of current node table in dict or json format. You can make custom query with attribute
query
. By default it return all nodes in table.- Args:
- session (
sqlalchemy.orm.session.Session
): SQLAlchemy session - Kwargs:
json (bool): if True return JSON jqTree format json_fields (function): append custom fields in JSON query (function): it takes
sqlalchemy.orm.query.Query
object as an argument, and returns in a modified formdef query(nodes): return nodes.filter(node.__class__.tree_id.is_(node.tree_id)) node.get_tree(session=DBSession, json=True, query=query)
Example:
sqlalchemy_mptt.tests.cases.get_tree.test_get_tree
sqlalchemy_mptt.tests.cases.get_tree.test_get_json_tree
sqlalchemy_mptt.tests.cases.get_tree.test_get_json_tree_with_custom_field
-
is_ancestor_of
(other, inclusive=False)[source]¶ class or instance level method which returns True if self is ancestor (closer to root) of other else False. Optional flag inclusive on whether or not to treat self as ancestor of self.
For example see:
sqlalchemy_mptt.tests.cases.integrity.test_hierarchy_structure
-
is_descendant_of
(other, inclusive=False)[source]¶ class or instance level method which returns True if self is descendant (farther from root) of other else False. Optional flag inclusive on whether or not to treat self as descendant of self.
For example see:
sqlalchemy_mptt.tests.cases.integrity.test_hierarchy_structure
-
leftsibling_in_level
()[source]¶ Node to the left of the current node at the same level
For example see
sqlalchemy_mptt.tests.cases.get_tree.test_leftsibling_in_level
-
move_after
(node_id)[source]¶ Moving one node of tree after another
For example see
sqlalchemy_mptt.tests.cases.move_node.test_move_after_function
-
move_before
(node_id)[source]¶ Moving one node of tree before another
For example see:
sqlalchemy_mptt.tests.cases.move_node.test_move_before_function
sqlalchemy_mptt.tests.cases.move_node.test_move_before_to_other_tree
sqlalchemy_mptt.tests.cases.move_node.test_move_before_to_top_level
-
move_inside
(parent_id)[source]¶ Moving one node of tree inside another
For example see:
sqlalchemy_mptt.tests.cases.move_node.test_move_inside_function
sqlalchemy_mptt.tests.cases.move_node.test_move_inside_to_the_same_parent_function
-
path_to_root
(session=None, order=<function desc>)[source]¶ Generate path from a leaf or intermediate node to the root.
For example:
node11.path_to_root()
level Nested sets example ----------------------------------------- 1 | 1(1)22 | ________|______|_____________________ | | | | | | | ------+--------- | | 2 2(2)5 6(4)11 | -- 12(7)21 | | ^ | / \ | 3 3(3)4 7(5)8 9(6)10 ---/---- \ | 13(8)16 | 17(10)20 | | | | | 4 14(9)15 | 18(11)19 | | | -------------
-