Adds hierarchical models to Django
Project description
django-hierarchical-models
This package provides several implementations Django models which support hierarchical data. Efficiently modeling hierarchical, or tree like data, in a relational database can be non-trivial. The following models implement the same interface, but there have different tradeoffs.
models.HierarchicalModelInterface
This abstract model defines the shared functionality of all hierarchical models. Some models may implement additional methods which are cheap for their implementation.
models.AdjacencyListModel
This is the most trivial implementation, using a single _parent
foreign key field.
Edits are efficient in this model, but queries for children/parents can be very
expensive.
models.PathEnumerationModel
This model uses a _ancestors
json field to store the path to its root. This model
has middle ground efficiency for edits and queries. NOTE: This model requires
database features that are not available in Oracle or SQLite backends. An exception
will be raised if you attempt to use this model with an unsupported backend.
models.NestedSetModel
This model uses _left
and _right
integer fields to determine which instances it is
parent/child to. Queries can be very efficient in this model, but edits can be very
expensive, possibly even requiring updates to _left
and _right
fields of ever model
instance for a single edit.
models.Node
Calls to HierarchicalModel.children()
return this type, which has instance
and
children
members, with the children being additional instances of Node
.
Benchmarks
These benchmarks are to illustrate the relative performance of the different models. As of now they are kind of whacked out. These tests were run with Postgres.
Model | Query Ancestors | Query Parent | Query Children | Query Direct Children | Create | Create Child | Delete | Delete Parent | Add Child | Remove Child | Set Parent | Set Parent Unchecked* |
---|---|---|---|---|---|---|---|---|---|---|---|---|
Adjacency List | 9.56 | 2.76 | 5.17 | 0.96 | 0.98 | 0.33 | 0.72 | 0.95 | 0.69 | 1.64 | 133.60 | 0.93 |
Path Enumeration | 9.17 | 2.67 | 29.71 | 0.84 | 0.99 | 0.32 | 0.75 | 1.26 | 0.94 | 1.79 | 2.98 | |
Nested Set | 169.86 | 118.97 | 211.75 | 107.30 | 3.31 | 59.19 | 113.79 | 175.11 | 71.13 | 293.61 | 572.47 |
* function only available on Adjacency List Model
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
Built Distribution
Hashes for django_hierarchical_models-1.2.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | ca28a362b81cac6c59dff2fe8c59de3cfd710e96ef5ff0477cf3ae038080ceb8 |
|
MD5 | 98659bdc198c4a9988969871c8ae9f9f |
|
BLAKE2b-256 | e4d93de76cfaaf313e595055bc0c9795432fef318c15675c053420ec0a13e564 |
Hashes for django_hierarchical_models-1.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0afc988e6dd5cf83727a994f91f3608e390303fdc70936142c3a6d3a80d6e3f0 |
|
MD5 | dfba69fd640ec7ecbbdece890ee46989 |
|
BLAKE2b-256 | cfe027b8b2d366349e369f5550542ffdf4108e1e5250255c6810a9dfba606af4 |