How it works...

The MPTTModel mixin will add the tree_id, lft, rght, and level fields to the Category model:

  • The tree_id field is used as you can have multiple trees in the database table. In fact, each root category is saved in a separate tree.
  • The lft and rght fields store the left and right values used in the MPTT algorithms.
  • The level field stores the node's depth in the tree. The root node will be level 0.

Through the order_insertion_by meta option specific to MPTT, we ensure that when new categories are added, they stay in alphabetical order by title.

Besides new fields, the MPTTModel mixin adds methods to navigate through the tree structure similar to how you navigate through DOM elements using JavaScript. These methods are as follows:

  • If you want to access the ancestors of a category, use the following code. Here,
    the ascending parameter defines from which direction to read the nodes (the default is False), and the include_self parameter defines whether to include the category itself in QuerySet (the default is False):
ancestor_categories = category.get_ancestors(
ascending=False,
include_self=False,
)
  • To just get the root category, use the following code:
root = category.get_root()
  • If you want to get the direct children of a category, use the following code:
children = category.get_children()
  • To get all of the descendants of a category, use the following code. Here, the include_self parameter again defines whether or not to include the category itself in QuerySet:
descendants = category.get_descendants(include_self=False)
  • If you want to get the descendant count without querying the database, use the following code:
descendants_count = category.get_descendant_count()
  • To get all siblings, call the following method:
siblings = category.get_siblings(include_self=False)
Root categories are considered siblings of other root categories.
  • To just get the previous and next siblings, call the following methods:
previous_sibling = category.get_previous_sibling()
next_sibling = category.get_next_sibling()
  • Also, there are methods to check whether the category is root, child, or leaf, as follows:
category.is_root_node()
category.is_child_node()
category.is_leaf_node()

All of these methods can be used either in views, templates, or management commands. If you want to manipulate the tree structure, you can also use the insert_at() and move_to() methods. In this case, you can read about them and the tree manager methods at https://django-mptt.readthedocs.io/en/stable/models.html.

In the preceding models, we used TreeForeignKey and TreeManyToManyField. These are similar to ForeignKey and ManyToManyField, except that they show the choices indented in hierarchies in the administration interface.

Also, note that in the Meta class of the Category model, we order the categories by tree_id and then by the lft value to show the categories naturally in the tree structure.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.35.81