Code documentation

class mptt2.enums.Position(value)

Simple enum to provide pre defined position choices

FIRST_CHILD: Tuple[str, str] = 'first-child'

the node shall be the leftmost child of the target

LAST_CHILD: Tuple[str, str] = 'last-child'

the node shall be the rightmost child of the target

LEFT: Tuple[str, str] = 'left'

the node shall be the left sibling of the target

RIGHT: Tuple[str, str] = 'right'

the node shall be the right sibling of the target

class mptt2.models.Tree(*args, **kwargs)

Simple Tree model to generate simple tree id’s by the database to support thread safe inserting new tree’s

exception DoesNotExist
exception MultipleObjectsReturned
id

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

objects = <django.db.models.manager.Manager object>
tests_othernode_nodes

Accessor to the related objects manager on the reverse side of a many-to-one relation.

In the example:

class Child(Model):
    parent = ForeignKey(Parent, related_name='children')

Parent.children is a ReverseManyToOneDescriptor instance.

Most of the implementation is delegated to a dynamically defined manager class built by create_forward_many_to_many_manager() defined below.

tests_simplenode_nodes

Accessor to the related objects manager on the reverse side of a many-to-one relation.

In the example:

class Child(Model):
    parent = ForeignKey(Parent, related_name='children')

Parent.children is a ReverseManyToOneDescriptor instance.

Most of the implementation is delegated to a dynamically defined manager class built by create_forward_many_to_many_manager() defined below.

class mptt2.models.Node(*args, **kwargs)

Abstract MPTT Node model, which implements all needed fields for nested sets.

Parameters:
  • mptt_parent (ForeignKey, optional) – A foreignkey to the parent node. None means it is the root node of a tree.

  • mptt_tree (ForeignKey) – A foreignkey to a unique tree object to differ between different trees.

  • mptt_lft (int) – The left value of the node

  • mptt_rgt (int) – The right value of the node

  • mptt_depth (int) – The hierarchy level of this node inside the tree

class Meta
abstract = False
constraints = [<CheckConstraint: check=(AND: ('mptt_rgt__gt', F(mptt_lft))) name='%(app_label)s_%(class)s_rgt_gt_lft'>]
indexes = [<Index: fields=['mptt_tree_id', 'mptt_lft', 'mptt_rgt']>]
ordering = ['mptt_tree_id', 'mptt_lft']
delete(*args, **kwargs)

Custom delete function to update nested set values if a node and there descendants are deleted.

property descendant_count: int

returns the descendant count

get_ancestors(include_self=False, asc=False) QuerySet

returns a queryset representing the ancestors of the current node

Parameters:
  • asc (bool) – switch to include the current node with the queryset

  • asc – switch to sort the queryset ascending

get_children(asc=False) QuerySet

returns a queryset representing the children of the current node

Parameters:

asc (bool) – switch to sort the queryset ascending

get_descendants(include_self=False, asc=False) QuerySet

returns a queryset representing the descendants of the current node

Parameters:
  • asc (bool) – switch to include the current node with the queryset

  • asc – switch to sort the queryset ascending

get_family(include_self=False, asc=False) QuerySet

returns a queryset representing the family of the current node (descendants and ancestors)

Parameters:
  • asc (bool) – switch to include the current node with the queryset

  • asc – switch to sort the queryset ascending

get_root()

returns the root node of the tree where this node is part of

get_siblings(include_self=False, asc=False) QuerySet

returns a queryset representing siblings of the current node

Parameters:
  • asc (bool) – switch to include the current node with the queryset

  • asc – switch to sort the queryset ascending

property has_leafs: bool

returns true if this node has leafs (descendants)

insert_at(target, position: Position = Position.LAST_CHILD)

Tree function to insert this node to a given target relative by the given position

Parameters:
  • target (mptt2.enums.Position, optional) – The target node where the given node shall be inserted relative to.

  • position – The relative position to the target (Default: Position.LAST_CHILD)

Returns:

the inserted node it self

Return type:

mptt2.models.Node

property is_leaf_node: bool

returns true if this is a leaf node without children

property is_root_node: bool

returns True if this is the root of the tree

move_to(target, position: Position = Position.LAST_CHILD)

Tree function to move a node relative to a given target by the given position

Parameters:
  • target (mptt2.models.Node) – The target node where the given node shall be inserted relative to.

  • position (mptt2.enums.Position, optional) – The relative position to the target (Default: Position.LAST_CHILD)

Returns:

the inserted node it self

Return type:

mptt2.models.Node

mptt_depth

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

mptt_lft

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

mptt_parent

Accessor to the related object on the forward side of a many-to-one or one-to-one (via ForwardOneToOneDescriptor subclass) relation.

In the example:

class Child(Model):
    parent = ForeignKey(Parent, related_name='children')

Child.parent is a ForwardManyToOneDescriptor instance.

mptt_parent_id
mptt_rgt

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

mptt_tree

Accessor to the related object on the forward side of a many-to-one or one-to-one (via ForwardOneToOneDescriptor subclass) relation.

In the example:

class Child(Model):
    parent = ForeignKey(Parent, related_name='children')

Child.parent is a ForwardManyToOneDescriptor instance.

mptt_tree_id
objects: TreeManager
property subtree_width: int

returns the width of the left and right attribute which are used from descendants

class mptt2.managers.TreeManager(*args, **kwargs)
get_queryset() QuerySet

Return a new QuerySet object. Subclasses can override this method to customize the behavior of the Manager.

insert_node(node, target=None, position: Position = Position.LAST_CHILD)

Tree function to insert this node to a given target relative by the given position

Parameters:
  • target (mptt2.enums.Position, optional) – The target node where the given node shall be inserted relative to.

  • position – The relative position to the target (Default: Position.LAST_CHILD)

Returns:

the inserted node it self

Return type:

mptt2.models.Node

move_node(node, target, position=Position.LAST_CHILD)

Tree function to move a node relative to a given target by the given position

Parameters:
  • target (mptt2.models.Node) – The target node where the given node shall be inserted relative to.

  • position (mptt2.enums.Position, optional) – The relative position to the target (Default: Position.LAST_CHILD)

Returns:

the inserted node it self

Return type:

mptt2.models.Node

queryset_class

alias of TreeQuerySet