Skip to main content

Efficient tree-based datastructure for Django

Project description

Build Status Code Health Badge Documentation Status Test Coverage Version Badge License Badge

django-closuretree is an implementation of the closure tree technique for Django applications designed to provide efficient querying of tree-based structures in a relational database. Its goal is to reduce the number of queries required when querying the children or parents of a given object.

Given the following model:

class Node(models.Model):
    name = models.CharField(max_length=24)
    parent = models.ForeignKey('self', related_name='children')

The children of each model can be queried with:

Node.objects.get(name='A').children.all()

However, for recursive lookups, this results in a large number of queries. Instead, django-closuretree allows you to extract them all in one go:

from closuretree.models import ClosureModel

class Node(ClosureModel):
    name = models.CharField(max_length=24)
    parent = models.ForeignKey('self', related_name='children')

Node.objects.get(name='A').get_descendants()

A single query will obtain all the descendants.

Quick Start

  • Install django-closuretree with pip install django-closuretree.

  • Inherit your models from closuretree.models.ClosureModel instead of django.db.models.Model.

That’s it. You can now use get_descendants() and get_ancestors() on a model instance.

If you’re adding this to an existing application that already has data in the database, you’ll need to run the rebuildtable() method of each model before the closure tree will be populated with the existing data:

Node.rebuildtable()

Contributing

To contribute, fork the repo, do your work, and issue a pull request. We ask that contributors adhere to PEP8 standards, and include full tests for all their code.

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-closuretree-1.1.8.tar.gz (12.1 kB view details)

Uploaded Source

Built Distributions

django_closuretree-1.1.8-py3-none-any.whl (11.4 kB view details)

Uploaded Python 3

django_closuretree-1.1.8-py2-none-any.whl (11.4 kB view details)

Uploaded Python 2

File details

Details for the file django-closuretree-1.1.8.tar.gz.

File metadata

File hashes

Hashes for django-closuretree-1.1.8.tar.gz
Algorithm Hash digest
SHA256 f5816351d3ef4034939f3ba015fad0227907d741a1a611561d740899f782dac1
MD5 90c237240fac4ae847da4d9437b1a08b
BLAKE2b-256 35c0c6dbaa052b8c12239fb7e74da9f4f2609aa7fd0a2e4be2860342b17884c2

See more details on using hashes here.

File details

Details for the file django_closuretree-1.1.8-py3-none-any.whl.

File metadata

File hashes

Hashes for django_closuretree-1.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 e0c89a55c4279070f192c2d90dbdc9198f226dfd38ae8e942af3a3747a5d76ff
MD5 0d6e79f79da7f1754ea4aaba3cbf490b
BLAKE2b-256 be5a19faef314178a2863346afd091d228dfd9bf837bfa6cdd2fbe83d9386707

See more details on using hashes here.

File details

Details for the file django_closuretree-1.1.8-py2-none-any.whl.

File metadata

File hashes

Hashes for django_closuretree-1.1.8-py2-none-any.whl
Algorithm Hash digest
SHA256 6ffa0d035592f21dc8c8877371186c197b698cecc01a966ac08bea4751177ba9
MD5 dce090bac486c1c135adac98b80ab7a1
BLAKE2b-256 dd38d3c3d3f80d4571595c2806ddec8a8988cf17e037308e4315e18d2146075f

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