Skip to main content

Tree queries with explicit opt-in, without configurability

Project description

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 2.2 or better, Python 3.6 or better. See the GitHub actions 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 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.

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.

  • 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_tree_queries-0.9.0.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

django_tree_queries-0.9.0-py3-none-any.whl (11.3 kB view details)

Uploaded Python 3

File details

Details for the file django_tree_queries-0.9.0.tar.gz.

File metadata

  • Download URL: django_tree_queries-0.9.0.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for django_tree_queries-0.9.0.tar.gz
Algorithm Hash digest
SHA256 9ed3b6aa399d326485a09b6c5c273fb595563b9ae209353582d6b08d5a7f455f
MD5 e13200f189fa6ea6c0e6091dc51c625d
BLAKE2b-256 284ad94e86a41a31550569f23936c2ebe61d486fdf4587de10a4943f70a53914

See more details on using hashes here.

File details

Details for the file django_tree_queries-0.9.0-py3-none-any.whl.

File metadata

  • Download URL: django_tree_queries-0.9.0-py3-none-any.whl
  • Upload date:
  • Size: 11.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for django_tree_queries-0.9.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d01ae9c0812502d924149353424eb56a08a99a01e84632aeba867457686c65d9
MD5 972838cc0d3dee95d1a29190f90f9661
BLAKE2b-256 bee37282551ef353eeffb23de5e5d1cbf6c06b21d3d322390ce85b9e0d0315f6

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page