Django MPTT

Utilities for implementing Modified Preorder Tree Traversal with your Django models.

Library
PyPI
v0.18.0
2,971stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
70/100Good
Development Activity56
Maintenance40
Community84
Maturity60
Momentum40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
76/100Good
Architecture82
Code Quality80
Innovation66
Learning Curve62

Django MPTT provides model fields, managers, and admin integration for implementing Modified Preorder Tree Traversal (MPTT) on Django models, making it easy to store and query hierarchical data such as categories, comment threads, and organizational trees. It stores pre-computed left, right, level, and tree_id values on each row so that reads of ancestors, descendants, and siblings are very fast.

The project is now in best-effort maintenance mode: bugs are fixed and compatibility with new Django versions is kept, but new features are not added and the maintainers suggest CTE-based alternatives for new projects. It remains widely used and well understood, with a mature API and thorough documentation.

What You Get

  • An MPTTModel base class plus TreeForeignKey and TreeManager for defining tree-structured models
  • Manager and queryset helpers for fetching ancestors, descendants, siblings, and subtrees
  • Admin integration (DraggableMPTTAdmin) and form fields for editing tree data
  • Template tags for rendering recursive tree structures in Django templates

Common Use Cases

  • Modeling nested categories or taxonomies with fast subtree queries
  • Building threaded comment or forum structures
  • Representing organizational charts, menus, or nested page hierarchies

Under The Hood

Architecture - The mptt package centers on models.py (MPTTModel, MPTTModelBase metaclass), fields.py (TreeForeignKey, TreeManyToManyField), managers.py and querysets.py (TreeManager, insertion/move logic), plus admin.py, forms.py, templatetags and signals. Each model row carries lft, rght, level and tree_id columns; inserts and moves recompute the affected sibling and ancestor bounds inside signal-driven manager methods, and rebuild() can recompute a whole tree from parent pointers.

Tech Stack - Pure Python packaged with pyproject.toml, targeting modern Django and Python versions. Testing runs through a Django test project under tests with tox for multi-version matrices; localization files ship under mptt/locale.

Code Quality - The codebase is mature and well-tested with an extensive tox matrix, but the README is candid that the pre-computed-bounds design has inherent write-amplification and concurrency hazards, which is why the project is now maintenance-only. Reads are fast; heavy concurrent writes require care and occasional rebuild().

API Design - The API is idiomatic Django: subclass MPTTModel, declare a TreeForeignKey, and use get_ancestors/get_descendants/get_family plus admin and template-tag helpers. The learning curve is moderate, mostly around understanding MPTT semantics and the constraints on bulk operations, and the documentation under docs is thorough.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search