mptt.utils

Utilities for working with lists of model instances which represent trees.

mptt.utils.drilldown_tree_for_node(node, rel_cls=None, rel_field=None, count_attr=None, cumulative=False, all_descendants=False)

Creates a drilldown tree for the given node. A drilldown tree consists of a node’s ancestors, itself and its immediate children or all descendants, all in tree order.

Optional arguments may be given to specify a Model class which is related to the node’s class, for the purpose of adding related item counts to the node’s children:

rel_cls

A Model class which has a relation to the node’s class.

rel_field

The name of the field in rel_cls which holds the relation to the node’s class.

count_attr

The name of an attribute which should be added to each child in the drilldown tree, containing a count of how many instances of rel_cls are related through rel_field.

cumulative

If True, the count will be for each child and all of its descendants, otherwise it will be for each child itself.

all_descendants

If True, return all descendants, not just immediate children.

mptt.utils.get_cached_trees(queryset)

Cache the parents and children on a queryset of MPTT instances. Args:

queryset:

List or queryset of _all_ the nodes that will have a cache. Instances not in the queryset will not be retrieved using the cache.

Notes:

Takes a list/queryset of model objects in MPTT (ordered by depth ASC) and caches the children and parent of every node. This allows up and down traversal through the tree without the need for further queries. Use cases include using a recursively included template or arbitrarily traversing trees. Any instance which has no parent in the provided queryset will be added to the top nodes list. For it to return proper results, queryset _must_ be ordered by ascending level.

Returns:

A list of top-level nodes, one node per tree root.

See Also:

Aliases to this function are also available: mptt.templatetags.mptt_tag.cache_tree_children

Use for recursive rendering in templates.

mptt.querysets.TreeQuerySet.get_cached_trees

Useful for chaining with queries; e.g., Node.objects.filter(**kwargs).get_cached_trees()

mptt.utils.previous_current_next(items)

From http://www.wordaligned.org/articles/zippy-triples-served-with-python

Creates an iterator which returns (previous, current, next) triples, with None filling in when there is no previous or next available.

mptt.utils.tree_item_iterator(items, ancestors=False, callback=<class 'str'>)

Given a list of tree items, iterates over the list, generating two-tuples of the current tree item and a dict containing information about the tree structure around the item, with the following keys:

'new_level'

True if the current item is the start of a new level in the tree, False otherwise.

'closed_levels'

A list of levels which end after the current item. This will be an empty list if the next item is at the same level as the current item.

If ancestors is True, the following key will also be available:

'ancestors'

A list of representations of the ancestors of the current node, in descending order (root node first, immediate parent last).

For example: given the sample tree below, the contents of the list which would be available under the 'ancestors' key are given on the right:

Books                    ->  []
   Sci-fi                ->  ['Books']
      Dystopian Futures  ->  ['Books', 'Sci-fi']

You can overload the default representation by providing an optional callback function which takes a single argument and performs coercion as required.