GithubHelp home page GithubHelp logo

benbacardi / django-closuretree Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ocadotechnology/django-closuretree

0.0 3.0 0.0 63 KB

Efficient tree-based datastructure for Django

License: Apache License 2.0

Python 87.15% Makefile 12.85%

django-closuretree's Introduction

django-closuretree

Build Status Code Health Badge Documentation Status

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.

django-closuretree's People

Contributors

benbacardi avatar mikebryant avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.