### Navigation

- index
- modules |
- next |
- previous |
- django-mptt 0.6.0 documentation »
- Autogenerated documentation »
- `mptt` »

# `mptt.models`

class `mptt.models.``MPTTModel`(*args, **kwargs)

  Base class for tree models.

  class `Meta`
    `abstract` = False
  `MPTTModel.``delete`(*args, **kwargs)

    Calling `delete` on a node will delete it as well as its full subtree, as opposed to reattaching all the subnodes to its parent node.

    There are no argument specific to a MPTT model, all the arguments will be passed directly to the django’s `Model.delete`.

    `delete` will not return anything.

  `MPTTModel.``get_ancestors`(ascending=False, include_self=False)

    Creates a `QuerySet` containing the ancestors of this model instance.

    This defaults to being in descending order (root ancestor first, immediate parent last); passing `True` for the `ascending` argument will reverse the ordering (immediate parent first, root ancestor last).

    If `include_self` is `True`, the `QuerySet` will also include this model instance.

  `MPTTModel.``get_children`()

    Returns a `QuerySet` containing the immediate children of this model instance, in tree order.

    The benefit of using this method over the reverse relation provided by the ORM to the instance’s children is that a database query can be avoided in the case where the instance is a leaf node (it has no children).

    If called from a template where the tree has been walked by the `cache_tree_children` filter, no database query is required.

  `MPTTModel.``get_descendant_count`()

    Returns the number of descendants this model instance has.

  `MPTTModel.``get_descendants`(include_self=False)

    Creates a `QuerySet` containing descendants of this model instance, in tree order.

    If `include_self` is `True`, the `QuerySet` will also include this model instance.

  `MPTTModel.``get_leafnodes`(include_self=False)

    Creates a `QuerySet` containing leafnodes of this model instance, in tree order.

    If `include_self` is `True`, the `QuerySet` will also include this model instance (if it is a leaf node)

  `MPTTModel.``get_level`()

    Returns the level of this node (distance from root)

  `MPTTModel.``get_next_sibling`(*filter_args, **filter_kwargs)

    Returns this model instance’s next sibling in the tree, or `None` if it doesn’t have a next sibling.

  `MPTTModel.``get_previous_sibling`(*filter_args, **filter_kwargs)

    Returns this model instance’s previous sibling in the tree, or `None` if it doesn’t have a previous sibling.

  `MPTTModel.``get_root`()

    Returns the root node of this model instance’s tree.

  `MPTTModel.``get_siblings`(include_self=False)

    Creates a `QuerySet` containing siblings of this model instance. Root nodes are considered to be siblings of other root nodes.

    If `include_self` is `True`, the `QuerySet` will also include this model instance.

  `MPTTModel.``insert_at`(target, position=u'first-child', save=False, allow_existing_pk=False)

    Convenience method for calling `TreeManager.insert_node` with this model instance.

  `MPTTModel.``is_ancestor_of`(other, include_self=False)

    Returns `True` if this model is an ancestor of the given node, `False` otherwise. If include_self is True, also returns True if the two nodes are the same node.

  `MPTTModel.``is_child_node`()

    Returns `True` if this model instance is a child node, `False` otherwise.

  `MPTTModel.``is_descendant_of`(other, include_self=False)

    Returns `True` if this model is a descendant of the given node, `False` otherwise. If include_self is True, also returns True if the two nodes are the same node.

  `MPTTModel.``is_leaf_node`()

    Returns `True` if this model instance is a leaf node (it has no children), `False` otherwise.

  `MPTTModel.``is_root_node`()

    Returns `True` if this model instance is a root node, `False` otherwise.

  `MPTTModel.``move_to`(target, position=u'first-child')

    Convenience method for calling `TreeManager.move_node` with this model instance.

    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.

  `MPTTModel.``save`(*args, **kwargs)

    If this is a new node, sets tree fields up before it is inserted into the database, making room in the tree structure as neccessary, defaulting to making the new node the last child of its parent.

    It the node’s left and right edge indicators already been set, we take this as indication that the node has already been set up for insertion, so its tree fields are left untouched.

    If this is an existing node and its parent has been changed, performs reparenting in the tree structure, defaulting to making the node the last child of its new parent.

    In either case, if the node’s class has its `order_insertion_by` tree option set, the node will be inserted or moved to the appropriate position to maintain ordering by the specified field.

class `mptt.models.``MPTTModelBase`

  Metaclass for MPTT models

  classmethod `register`(meta, cls, **kwargs)

    For the weird cases when you need to add tree-ness to an existing class. For other cases you should subclass MPTTModel instead of calling this.

class `mptt.models.``MPTTOptions`(opts=None, **kwargs)

  Options class for MPTT models. Use this as an inner class called `MPTTMeta`:

      class MyModel(MPTTModel):
          class MPTTMeta:
              order_insertion_by = ['name']
              parent_attr = 'myparent'

  `get_ordered_insertion_target`(node, parent)

    Attempts to retrieve a suitable right sibling for `node` underneath `parent` (which may be `None` in the case of root nodes) so that ordering by the fields specified by the node’s class’ `order_insertion_by` option is maintained.

    Returns `None` if no suitable sibling can be found.

  `get_raw_field_value`(instance, field_name)

    Gets the value of the given fieldname for the instance. This is not the same as getattr(). This function will return IDs for foreignkeys etc, rather than doing a database query.

  `insertion_target_filters`(instance, order_insertion_by)

    Creates a filter which matches suitable right siblings for `node`, where insertion should maintain ordering according to the list of fields in `order_insertion_by`.

    For example, given an `order_insertion_by` of `['field1', 'field2', 'field3']`, the resulting filter should correspond to the following SQL:

        field1 > %s
        OR (field1 = %s AND field2 > %s)
        OR (field1 = %s AND field2 = %s AND field3 > %s)

  `left_attr` = u'lft'
  `level_attr` = u'level'
  `order_insertion_by` = []
  `parent_attr` = u'parent'
  `right_attr` = u'rght'
  `set_raw_field_value`(instance, field_name, value)

    Sets the value of the given fieldname for the instance. This is not the same as setattr(). This function requires an ID for a foreignkey (etc) rather than an instance.

  `tree_id_attr` = u'tree_id'
  `update_mptt_cached_fields`(instance)
    Caches (in an instance._mptt_cached_fields dict) the original values of:
      - parent pk
      - fields specified in order_insertion_by

    These are used in pre_save to determine if the relevant fields have changed, so that the MPTT fields need to be updated.

`mptt.models.``classproperty`

`mptt.models.``classpropertytype`

### Navigation

- index
- modules |
- next |
- previous |
- django-mptt 0.6.0 documentation »
- Autogenerated documentation »
- `mptt` »