Skip to main content

Tree queries with explicit opt-in, without configurability

Project description

https://travis-ci.org/matthiask/django-tree-queries.svg?branch=master

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.8, 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.

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.

  • 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.4.2.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_tree_queries-0.4.2-py2.py3-none-any.whl (10.4 kB view details)

Uploaded Python 2Python 3

File details

Details for the file django-tree-queries-0.4.2.tar.gz.

File metadata

  • Download URL: django-tree-queries-0.4.2.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.2

File hashes

Hashes for django-tree-queries-0.4.2.tar.gz
Algorithm Hash digest
SHA256 806fa670709c4df90021a6635ddc9ddc6eb12b146c57ac554cb37c4ff10bebff
MD5 1743f4958fc3698ad4f38f2ec1111930
BLAKE2b-256 14495f48167c73fdc417e959eca2486a6c0dc01abfc0c61a1f02c65270ac5186

See more details on using hashes here.

File details

Details for the file django_tree_queries-0.4.2-py2.py3-none-any.whl.

File metadata

  • Download URL: django_tree_queries-0.4.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 10.4 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.2

File hashes

Hashes for django_tree_queries-0.4.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 62fcdd4b8769f4ebdf5711fbcd0dfcefad891a4a9474c38ebfbc6c8fb5d4fda8
MD5 7b544fae6ac07a4f1b7918d7209dcf92
BLAKE2b-256 7d7497362fc432e72f8a0ae9b1aaa16b964c79c35db710e461433df49eb92596

See more details on using hashes here.

Supported by

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