Creating hierarchical categories with django-treebeard

There are several algorithms for tree structures, each with its own benefits. An app called django-treebeard, an alternative to django-mptt, which is used by Django CMS, provides support for three tree forms:

  • Adjacency List trees are simple structures, where each node has a parent attribute. Although read operations are fast, this comes at the cost of slow writes.
  • Nested Sets trees and MPTT trees are the same; they structure nodes as sets nested beneath the parent. This structure also provides very fast read access, at the cost of more expensive writing and deletion, particularly when writes require some particular ordering.
  • Materialized Path trees are built with each node in the tree having an associated path attribute, which is a string indicating the full path from the root to the nodeā€”much like a URL path indicates where to find a particular page on a website. This is the most efficient approach supported.

As a demonstration of the support it has for all of these algorithms, we will use django-treebeard and its consistent API. We will extend the ideas app from the Implementing a multilingual search with Haystack and Whoosh recipe in Chapter 3Forms and Views. In our changes, we will simply enhance the Category model with support for hierarchy via one of the supported tree algorithms.

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

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