Skip to content

st8st8/django-closuretree

 
 

Repository files navigation

django-closuretree

Build Status

Test Coverage

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.

This is a folk of the original work by Ocado Technology with a few extra features from fork https://github.com/ykiu/django-closuretree.

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 git+https://github.com/sam-mi/django-closuretree.git.
  • 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

  1. Fork the repo and create your branch from master.
  2. Do your work.
  3. Run tests (setup.py test). Dependencies will be installed into ./.eggs/. No need to explicitly activate a virtual environment.
  4. Make a PR.

We ask that contributors adhere to PEP8 standards, and include full tests for all their code.

About

Efficient tree-based datastructure for Django

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 95.5%
  • HTML 4.5%