mptt.managers

A custom manager for working with trees of objects.

class mptt.managers.TreeManager

A manager for working with trees of objects.

Adds a related item count to a given QuerySet using its extra method, for a Model class which has a relation to this Manager‘s Model class.

Arguments:

rel_model
A Model class which has a relation to this Manager`‘s Model class.
rel_field
The name of the field in rel_model which holds the relation.
count_attr
The name of an attribute which should be added to each item in this QuerySet, containing a count of how many instances of rel_model are related to it through rel_field.
cumulative
If True, the count will be for each item and all of its descendants, otherwise it will be for each item itself.
get_query_set()

Returns a QuerySet which contains all tree items, ordered in such a way that that root nodes appear in tree id order and their subtrees appear in depth-first order.

init_from_model(model)

Sets things up. This would normally be done in contribute_to_class(), but Django calls that before we’ve created our extra tree fields on the model (which we need). So it’s done here instead, after field setup.

insert_node(node, target, position='last-child', save=False, allow_existing_pk=False)

Sets up the tree state for node (which has not yet been inserted into in the database) so it will be positioned relative to a given target node as specified by position (when appropriate) it is inserted, with any neccessary space already having been made for it.

A target of None indicates that node should be the last root node.

If save is True, node‘s save() method will be called before it is returned.

NOTE: This is a low-level method; it does NOT respect MPTTMeta.order_insertion_by. In most cases you should just set the node’s parent and let mptt call this during save.

left_attr
level_attr
move_node(node, target, position='last-child')

Moves node relative to a given target node as specified by position (when appropriate), by examining both nodes and calling the appropriate method to perform the move.

A target of None indicates that node should be turned into a root node.

Valid values for position are 'first-child', 'last-child', 'left' or 'right'.

node will be modified to reflect its new tree state in the database.

This method explicitly checks for node being made a sibling of a root node, as this is a special case due to our use of tree ids to order root nodes.

NOTE: This is a low-level method; it does NOT respect MPTTMeta.order_insertion_by. In most cases you should just move the node yourself by setting node.parent.

parent_attr
rebuild()

Rebuilds whole tree in database using parent link.

right_attr
root_node(tree_id)

Returns the root node of the tree with the given id.

root_nodes()

Creates a QuerySet containing root nodes.

tree_id_attr

Previous topic

mptt.forms

Next topic

mptt.models

This Page