In this chapter, we will cover the following recipes:
- Creating hierarchical categories
- Creating a category administration interface with django-mptt-admin
- Creating a category administration interface with django-mptt-tree-editor
- Rendering categories in a template
- Using a single selection field to choose a category in forms
- Using a checkbox list to choose multiple categories in forms
Whether you build your own forum, threaded comments, or categorization system, there will be a moment when you need to save hierarchical structures in the database. Although the tables of relational databases (such as MySQL and PostgreSQL) are of a flat manner, there is a fast and effective way to store hierarchical structures. It is called Modified Preorder Tree Traversal (MPTT). MPTT allows you to read the tree structures without recursive calls to the database.
At first, let's get familiar with the terminology of the tree structures. A tree data structure...