Coder Social home page Coder Social logo

django-closure-tree's Introduction

Django closure tree model.

Abstract base model for creating a Closure Tree using a recursive Postgres view.

https://schinckel.net/2016/01/27/django-trees-via-closure-view/

Usage

Inherit from the Node model:

from closure_tree.models import Node


class MyNode(Node):
    name = models.CharField(max_length=30)

Create migrations:

$ ./manage.py makemigrations

Add the CreateTreeClosure migration step:

$ ./manage.py makemigrations --empty myapp
from closure_tree.migrations import CreateTreeClosure

class Migration(migrations.Migration):

    dependencies = [
        ('dummy', '0001_initial'),
    ]

    operations = [
        CreateTreeClosure('MyNode'),
    ]

django-closure-tree's People

Contributors

funkybob avatar schinckel avatar wooble avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

django-closure-tree's Issues

Materialised Path View

It's also possible to have a materialised path view that does the same basic thing as this - but is that too much feature creep?

CREATE OR REPLACE RECURSIVE VIEW {view}(node_id, ancestors) AS

SELECT node_id, ARRAY[]::INTEGER[] AS ancestors
FROM {table} WHERE parent_id IS NULL

UNION ALL

SELECT node_id, ancestors || parent_id
FROM {table}
INNER JOIN {view} ON (parent_id = node_id);

It seems this may be useful - in the case of fetching the list of ancestors, it could then be done without building the other view (and then aggregating that, and remembering to include ordering).

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.