Skip to content

A tree library for Django, using PostgreSQL's ltree data type

Notifications You must be signed in to change notification settings

daisylb/django-pgtree

Repository files navigation

django-pgtree

A generic model for storing heirachial data in trees, using PostgreSQL's built-in ltree data type, plus a ltree field for you to use on your own models.

Supports Django 2.0+ on Python 3.5+.

from django.db import models
from django_pgtree.models import TreeNode

class Organism(TreeNode):
    name = models.CharField()
>>> animal = Organism.objects.create(name="Animal")
>>> mammal = Organism.objects.create(name="Mammal", parent=animal)
>>> dog = Organism.objects.create(name="Dog", parent=mammal)
>>> cat = Organism.objects.create(name="Cat", parent=mammal)

>>> mammal.children
[<Organism: Dog>, <Organism: Cat>]
>>> dog.ancestors
[<Organism: Animal>, <Organism: Mammal>]
>>> cat.siblings
[<Organism: Dog>]

About

A tree library for Django, using PostgreSQL's ltree data type

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages