python-django-tree-queries
Port variant v12
Summary Tree queries with explicit opt-in (3.12)
Package version 0.20.0
Homepage https://github.com/matthiask/django-tree-queries/
Keywords python
Maintainer Python Automaton
License Not yet specified
Other variants v13
Ravenports Buildsheet | History
Ravensource Port Directory | History
Last modified 13 JUN 2025, 00:30:19 UTC
Port created 14 JAN 2025, 19:34:39 UTC
Subpackage Descriptions
single =================== django-tree-queries =================== :alt: CI Status 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 3.2 or better, Python 3.8 or better. See the GitHub actions build for more details. Features and limitations ======================== - Supports only integer and UUID primary keys (for now). - 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. Note that the contents of the tree_path and tree_ordering are subject to change. You shouldn't rely on their contents. - Besides adding the fields mentioned above the package only adds queryset methods for ordering siblings and 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 do not support arrays and require us to provide a maximum length for the tree_path and tree_ordering upfront. - **Performance optimization**: The library automatically detects simple cases (single field ordering, no tree filters, no custom tree fields) and uses an optimized CTE that avoids creating a rank table, significantly improving performance for basic tree queries. Here's a blog post offering some additional insight (hopefully) into the reasons for `django-tree-queries' existence `_. Usage ===== - 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. - Call the ``order_siblings_by("field_name")`` queryset method if you want to order tree siblings by a specific model field. Note that Django's standard ``order_by()`` method isn't supported -- nodes are returned according to the `depth-first search algorithm `__. - Use ``tree_filter() and tree_exclude()`` for better performance when working with large tables - these filter the base table before building the tree structure. - Use ``tree_fields()`` to aggregate ancestor field values into arrays. - 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, or check the recipes below. Recipes ======= Basic models ~~~~~~~~~~~~ The following two examples both extend the TreeNode which offers a few agreeable utilities and a model validation method that prevents loops in the tree structure. The common table expression could be hardened against such loops but this would involve a performance hit which we don't want -- this is a documented limitation (non-goal) of the library after all.
Configuration Switches (platform-specific settings discarded)
PY312 ON Build using Python 3.12 PY313 OFF Build using Python 3.13
Package Dependencies by Type
Build (only) python312:dev:std
python-pip:single:v12
autoselect-python:single:std
Build and Runtime python312:primary:std
Download groups
main mirror://PYPIWHL/a4/7f/cbc208164239904b4a00065eec327d02071ef6c950c8f2d2288150b5ab33
Distribution File Information
56e14105a894b801e41da82ef9e82151d06b1f481cfe39f792c08b6c87c49fce 21146 python-src/django_tree_queries-0.20.0-py3-none-any.whl
Ports that require python-django-tree-queries:v12
No other ports depend on this one.