Skip to the content.

Core Concepts of Django Hierarchical Models

This document explains the fundamental concepts behind hierarchical models and how inheritance works in this system.

Shadow Fields

Hierarchical models use shadow fields (prefixed with an underscore) to store actual values in the database. These shadow fields serve as the storage mechanism for overridden values at each level in the hierarchy.

Property Accessors

The mixin automatically creates property accessors for each shadow field. These properties handle the inheritance logic, looking up the hierarchy when a value is not found at the current level.

Hierarchical Parent Relationship

Each model defines a relationship to its parent in the hierarchy using the hierarchical_parent property. This tells the system where to look for inherited values.

Inheritance Logic

When a property is accessed:

  1. The system first checks if the model has an override (value in shadow field)
  2. If no override exists, it looks at the parent model
  3. This continues recursively until a value is found or the hierarchy is exhausted

M2M Relationship Handling

For Many-to-Many relationships, special handling ensures that relationships can also be inherited through the hierarchy, with similar override capabilities.

Detailed Explanations

More detailed explanations of each concept will be added here.