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
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
-
Install the package:
pip install django-ltree
-
Add to your
INSTALLED_APPS:INSTALLED_APPS = [ ... "django_ltree", ... ]
-
Run migrations to install the ltree extension:
python manage.py migrate django_ltree
-
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
-
Add a PathField to your model:
from django_ltree.fields import PathField class Category(models.Model): name = models.CharField(max_length=50) path = PathField()
-
Create tree nodes:
root = Category.objects.create(name="Root", path="root") child = Category.objects.create(name="Child", path=f"{root.path}.child")
-
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
- Source Code: https://github.com/mariocesar/django-ltree
- Bug Reports: https://github.com/mariocesar/django-ltree/issues
- PyPI Package: https://pypi.org/project/django-ltree/
- PostgreSQL ltree Docs: https://www.postgresql.org/docs/current/ltree.html
Contributing
Contributions are welcome! Please see CONTRIBUTING.md for guidelines.
License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8ed3d92eb4b1e2cc28082ec96a965cbc302c6ac9080778536d93b18861dfe122
|
|
| MD5 |
3063a039b9735925820628d6bee96463
|
|
| BLAKE2b-256 |
3b853eeac295d6aec12dbbc11f45e54c8777edd5771db00cfea4321f461940bd
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0eddae2400e853cb4c747e7251035c8022b2c6ed23c68e9f733a5a04ed543c1a
|
|
| MD5 |
988d52660fce01d7b418a7787fed81df
|
|
| BLAKE2b-256 |
46e7871fc762dfaaedbb050f0f16b471e2270b36ca1a079a37ced8a3d66c2519
|