Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.pytest_cache
.tox
docs/_build
.vscode
4 changes: 3 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
django-pgtree
=============

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

Supports Django 2.0+ on Python 3.5+.

Expand Down Expand Up @@ -29,3 +29,5 @@ Supports Django 2.0+ on Python 3.5+.
[<Organism: Animal>, <Organism: Mammal>]
>>> cat.siblings
[<Organism: Dog>]
>>> Organism.objects.roots()
[<Organism: Animal>]
12 changes: 12 additions & 0 deletions django_pgtree/dbext.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from django.contrib.postgres.operations import CreateExtension


class LtreeExtension(CreateExtension):
"""Django migration operation to add PostgreSQL extension LTree to our project."""

def __init__(self):
self.name = 'ltree'

def database_backwards(self, app_label, schema_editor, from_state, to_state):
# No need to drop extension
pass
11 changes: 2 additions & 9 deletions docs/index.rst
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@

.. include:: ../README.rst

.. toctree::
:maxdepth: 2

install



Indices and tables
==================

* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`
model
2 changes: 1 addition & 1 deletion docs/install.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ On your development database and when you first deploy, you'll also need to run

CREATE EXTENSION ltree;

This command needs to be run by the database superuser (usually the ``postgres`` user). If you know that your migrations will be run by this user, you can create a ``RunSQL`` migration that does this for you, but in most cases you'll need to do this when you create the database.
This command needs to be run by the database superuser (usually the ``postgres`` user). If you know that your migrations will be run by this user, you can use our ``LtreeExtension`` migration operation (see more in :doc:`model`), but in most cases you'll need to do this when you create the database.

.. tip::

Expand Down
15 changes: 14 additions & 1 deletion docs/model.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,20 @@ This functionality is for when you want to model your data as a tree, of which m
class Organism(TreeNode):
name = models.CharField()

You can create root nodes as you would any other normal model:
If you haven't manually run the SQL query ``CREATE EXTENSION ltree`` before, you can do it in migration script:

.. code-block:: python

from django_pgtree.dbext import LtreeExtension

class Migration(migrations.Migration):
operations = [
LtreeExtension(),
# Other migration operations
]


You can create root nodes as you would like any other normal model:

.. code-block:: python

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tool.poetry]
name = "django-pgtree"
version = "0.1.4"
description = "Heirachial data for Django with Postgres."
description = "Hierarchical data for Django with Postgres."
authors = ["Adam Brenecki"]
license = "ISC"

Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ python_paths=testproject/

[flake8]
max_line_length = 88
ignore = W503