diff --git a/.gitignore b/.gitignore index 1bc5e58..0e8b022 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .pytest_cache .tox docs/_build +.vscode diff --git a/README.rst b/README.rst index 51d5eaf..d0fb6de 100644 --- a/README.rst +++ b/README.rst @@ -1,7 +1,7 @@ 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. +A generic model for storing hierarchical 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+. @@ -29,3 +29,5 @@ Supports Django 2.0+ on Python 3.5+. [, ] >>> cat.siblings [] + >>> Organism.objects.roots() + [] diff --git a/django_pgtree/dbext.py b/django_pgtree/dbext.py new file mode 100644 index 0000000..0e4528f --- /dev/null +++ b/django_pgtree/dbext.py @@ -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 diff --git a/docs/index.rst b/docs/index.rst index 6b3c3ff..b4e15a9 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -1,15 +1,8 @@ + .. include:: ../README.rst .. toctree:: :maxdepth: 2 install - - - -Indices and tables -================== - -* :ref:`genindex` -* :ref:`modindex` -* :ref:`search` + model diff --git a/docs/install.rst b/docs/install.rst index 013d845..1308695 100644 --- a/docs/install.rst +++ b/docs/install.rst @@ -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:: diff --git a/docs/model.rst b/docs/model.rst index f5bbf10..d897589 100644 --- a/docs/model.rst +++ b/docs/model.rst @@ -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 diff --git a/pyproject.toml b/pyproject.toml index 75f423b..c3ee8e3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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" diff --git a/tox.ini b/tox.ini index df29269..87f9e7d 100644 --- a/tox.ini +++ b/tox.ini @@ -34,3 +34,4 @@ python_paths=testproject/ [flake8] max_line_length = 88 +ignore = W503