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', null=True)

a = Node.objects.create(name='A')
Node.objects.create(name='B', parent=a)

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.2.0.tar.gz (12.7 kB view details)

Uploaded Source

Built Distributions

django_closuretree-1.2.0-py3-none-any.whl (12.0 kB view details)

Uploaded Python 3

django_closuretree-1.2.0-py2-none-any.whl (12.0 kB view details)

Uploaded Python 2

File details

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

File metadata

File hashes

Hashes for django-closuretree-1.2.0.tar.gz
Algorithm Hash digest
SHA256 570e9e05647790161632f833f82de7c51679333eb6e3cd26a01c1c359dc41221
MD5 a310f64f6b0267d009fe67ae225558fc
BLAKE2b-256 5322c674ae07363ac95189da024a5a70167b5841107912117508fc5343c96c79

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_closuretree-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2c95e04838e88e178b8d586986107ffe55fbe76dc06517005664a7061d078caa
MD5 9efc71899e5f0e5362cf8f48cd139501
BLAKE2b-256 ddd7291593199ef5a502ea8cb02b010cdf8b379fd2e188bd41c1c6a2ba035047

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_closuretree-1.2.0-py2-none-any.whl
Algorithm Hash digest
SHA256 98df2492e513ccd3c8ae6c04f0aa487d2b1adb571622bb8e42aee8ffd33a4f96
MD5 b9543fd70effb0ab7f8b7c4eb0aef0cd
BLAKE2b-256 a3b1b6672596799a576a9cef096a41c3c8f22af69e32c984d9d3e91e3d7bf32b

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