Skip to content

Latest commit

 

History

History
31 lines (22 loc) · 951 Bytes

README.rst

File metadata and controls

31 lines (22 loc) · 951 Bytes

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>]