Skip to main content

Django app to support ltree postgres extension

Project description

django-ltree

A Django implementation for PostgreSQL's ltree extension, providing efficient storage and querying of hierarchical tree-like data.

See PostgreSQL's ltree documentation to learn more about it.

The main benefits of ltree:

  • Efficient path queries (ancestors, descendants, pattern matching)
  • Index-friendly hierarchical storage
  • Powerful label path searching
  • Native PostgreSQL performance for tree operations

Test PyPI Version

Features

  • Django model fields for ltree data types
  • Query utilities for common tree operations
  • Migration support for ltree extension installation
  • Compatibility with Django's ORM and query syntax

Requirements

  • Django 4.2+
  • Python 3.11+
  • PostgreSQL 14+ (with ltree extension enabled)

Installation

  1. Install the package:

    pip install django-ltree
    
  2. Add to your INSTALLED_APPS:

    INSTALLED_APPS = [
        ...
        "django_ltree",
        ...
    ]
    
  3. Run migrations to install the ltree extension:

    python manage.py migrate django_ltree
    
  4. Alternatively you can avoid install the application, and create the the extensions with a custom migration in an app in your project.

    from django.db import migrations
    from django_ltree.operations import LtreeExtension
    
    class Migration(migrations.Migration):
        initial = True
        dependencies = []
    
        operations = [LtreeExtension()]
    

Quick Start

  1. Add a PathField to your model:

    from django_ltree.fields import PathField
    
    class Category(models.Model):
        name = models.CharField(max_length=50)
        path = PathField()
    
  2. Create tree nodes:

    root = Category.objects.create(name="Root", path="root")
    child = Category.objects.create(name="Child", path=f"{root.path}.child")
    
  3. Query ancestors and descendants:

    # Get all ancestors
    Category.objects.filter(path__ancestor=child.path)
    
    # Get all descendants
    Category.objects.filter(path__descendant=root.path)
    

Migration Dependency

Include django_ltree as a dependency in your app's migrations:

class Migration(migrations.Migration):
    dependencies = [
        ("django_ltree", "__latest__"),
    ]

Known Issues

  • Since PostgreSQL 16, the - character is also permitted. The tests in this repository expect version 16 or higher. If you're using PostgreSQL 15 or earlier, make sure to exclude - from labels.

Documentation

For complete documentation, see [TODO: Add Documentation Link].

Links

Contributing

Contributions are welcome! Please see CONTRIBUTING.md for guidelines.

License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_ltree-0.6.0.tar.gz (11.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_ltree-0.6.0-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

Details for the file django_ltree-0.6.0.tar.gz.

File metadata

  • Download URL: django_ltree-0.6.0.tar.gz
  • Upload date:
  • Size: 11.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for django_ltree-0.6.0.tar.gz
Algorithm Hash digest
SHA256 8ed3d92eb4b1e2cc28082ec96a965cbc302c6ac9080778536d93b18861dfe122
MD5 3063a039b9735925820628d6bee96463
BLAKE2b-256 3b853eeac295d6aec12dbbc11f45e54c8777edd5771db00cfea4321f461940bd

See more details on using hashes here.

File details

Details for the file django_ltree-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: django_ltree-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 9.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for django_ltree-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0eddae2400e853cb4c747e7251035c8022b2c6ed23c68e9f733a5a04ed543c1a
MD5 988d52660fce01d7b418a7787fed81df
BLAKE2b-256 46e7871fc762dfaaedbb050f0f16b471e2270b36ca1a079a37ced8a3d66c2519

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page