Skip to main content
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.

Release files for django-closuretree 1.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-closuretree 1.2.0
File Size Uploaded
django-closuretree-1.2.0.tar.gz 12.7 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for django-closuretree 1.2.0
File Interpreter ABI Platform
django_closuretree-1.2.0-py3-none-any.whl Python 3 none any Details
django_closuretree-1.2.0-py2-none-any.whl Python 2 none any Details

Total release size: 36.7 kB

Release files / django-closuretree-1.2.0.tar.gz

Download URL django-closuretree-1.2.0.tar.gz
Size 12.7 kB
Tags Source
SHA-256 checksum
How to use checksums
570e9e05647790161632f833f82de7c51679333eb6e3cd26a01c1c359dc41221
BLAKE2b-256 checksum
How to use checksums
5322c674ae07363ac95189da024a5a70167b5841107912117508fc5343c96c79
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / django_closuretree-1.2.0-py3-none-any.whl

Download URL django_closuretree-1.2.0-py3-none-any.whl
Size 12.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2c95e04838e88e178b8d586986107ffe55fbe76dc06517005664a7061d078caa
BLAKE2b-256 checksum
How to use checksums
ddd7291593199ef5a502ea8cb02b010cdf8b379fd2e188bd41c1c6a2ba035047
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / django_closuretree-1.2.0-py2-none-any.whl

Download URL django_closuretree-1.2.0-py2-none-any.whl
Size 12.0 kB
Tags Python 2
SHA-256 checksum
How to use checksums
98df2492e513ccd3c8ae6c04f0aa487d2b1adb571622bb8e42aee8ffd33a4f96
BLAKE2b-256 checksum
How to use checksums
a3b1b6672596799a576a9cef096a41c3c8f22af69e32c984d9d3e91e3d7bf32b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

1.2.0 This release

3 release files

1.1.10

2 release files

1.1.9

2 release files

1.1.8

3 release files

1.1.7

3 release files

1.1.6

3 release files

1.1.5

3 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page