Query Django model trees using adjacency lists and recursive common table expressions. Supports PostgreSQL, sqlite3 (3.8.3 or higher) and MariaDB (10.2.2 or higher) and MySQL (8.0 or higher, if running without ONLY_FULL_GROUP_BY).

Supports Django 1.11 or better, Python 2.7 and 3.4 or better. See the Travis CI build for more details.

Features and limitations

  • Supports only integer primary keys.
  • Allows specifying ordering among siblings.
  • Uses the correct definition of depth, where root nodes have a depth of zero.
  • The parent foreign key must be named "parent" at the moment (but why would you want to name it differently?)
  • The fields added by the common table expression always are tree_depth, tree_path and tree_ordering. The names cannot be changed. tree_depth is an integer, tree_path an array of primary keys and tree_ordering an array of values used for ordering nodes within their siblings.
  • Besides adding the fields mentioned above the package only adds queryset methods for filtering ancestors and descendants. Other features may be useful, but will not be added to the package just because it’s possible to do so.
  • Little code, and relatively simple when compared to other tree management solutions for Django. No redundant values so the only way to end up with corrupt data is by introducing a loop in the tree structure (making it a graph). The TreeNode abstract model class has some protection against this.
  • Supports only trees with max. 50 levels on MySQL/MariaDB, since those databases to not support arrays and require us to provide a maximum length for the tree_path and tree_ordering upfront.


  • Install django-tree-queries using pip.
  • Extend tree_queries.models.TreeNode or build your own queryset and/or manager using tree_queries.query.TreeQuerySet. The TreeNode abstract model already contains a parent foreign key for your convenience and also uses model validation to protect against loops.
  • Call the with_tree_fields() queryset method if you require the additional fields respectively the CTE.
  • Create a manager using TreeQuerySet.as_manager(with_tree_fields=True) if you want to add tree fields to queries by default.
  • Until documentation is more complete I’ll have to refer you to the test suite for additional instructions and usage examples.

Change log

Next version

  • Added support for adding tree fields to queries by default. Create a manager using TreeQuerySet.as_manager(with_tree_fields=True).
  • Ensured the availability of the with_tree_fields configuration also on subclassed managers, e.g. those used for traversing reverse relations.
  • Dropped compatibility with Django 1.8 to avoid adding workarounds to the testsuite.

0.4 (2020-09-13)

  • Fixed a grave bug where a position of 110 would be sorted before 20 for obvious reasons.
  • Added a custom TreeNodeForeignKey.deconstruct method to avoid migrations because of changing field types.
  • Removed one case of unnecessary fumbling in Query’s internals making things needlessly harder than they need to be. Made django-tree-queries compatible with Django’s master branch.
  • Removed Python 3.4 from the Travis CI job list.
  • Dropped the conversion of primary keys to text on PostgreSQL. It’s a documented constraint that django-tree-queries only supports integer primary keys, therefore the conversion wasn’t necessary at all.
  • Reverted to using integer arrays on PostgreSQL for ordering if possible instead of always converting everything to padded strings.

0.3 (2018-11-15)

  • Added a label_from_instance override to the form fields.
  • Removed the limitation that nodes can only be ordered using an integer field within their siblings.
  • Changed the representation of tree_path and tree_ordering used on MySQL/MariaDB and sqlite3. Also made it clear that the representation isn’t part of the public interface of this package.

0.2 (2018-10-04)

  • Added an optional argument to TreeQuerySet.with_tree_fields() to allow reverting to a standard queryset (without tree fields).
  • Added tree_queries.fields.TreeNodeForeignKey, tree_queries.forms.TreeNodeChoiceField and tree_queries.forms.TreeNodeMultipleChoiceField with node depth visualization.
  • Dropped Python 3.4 from the CI.

0.1 (2018-07-30)

  • Initial release!