-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4e1c5dd
commit cff4a2d
Showing
9 changed files
with
241 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from django.db import models | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
if TYPE_CHECKING: | ||
from .models import TreeModel | ||
|
||
|
||
class TreeQuerySet(models.QuerySet): | ||
def roots(self) -> models.QuerySet["TreeModel"]: | ||
return self.filter(path__depth=1) | ||
|
||
def children(self, path: str) -> models.QuerySet["TreeModel"]: | ||
return self.filter(path__descendants=path, path__depth=len(path) + 1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
Installation | ||
============ | ||
|
||
Requirements | ||
------------ | ||
|
||
Python 3.10 to 3.12 supported. | ||
|
||
Django 4.2 to 5.0 supported. | ||
|
||
|
||
Installation | ||
------------ | ||
|
||
1. Install with **pip**: | ||
|
||
.. code-block:: sh | ||
python -m pip install django-ltree-2 | ||
2. Add django-ltree to your ``INSTALLED_APPS``: | ||
|
||
.. code-block:: python | ||
INSTALLED_APPS = [ | ||
..., | ||
"django_ltree", | ||
..., | ||
] | ||
3. Run migrations: | ||
|
||
.. code-block:: sh | ||
./manage.py migrate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
Manager | ||
======= | ||
|
||
|
||
.. currentmodule:: django_ltree.managers | ||
|
||
.. class:: TreeManager | ||
|
||
This manager augments the django model, allowing it to be queried with tree specific queries | ||
|
||
.. automethod:: get_queryset | ||
|
||
.. automethod:: roots | ||
|
||
.. automethod:: children | ||
|
||
.. automethod:: create_child |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
Usage | ||
===== | ||
|
||
Lets assume that our model looks like this. | ||
|
||
.. code-block:: python | ||
# models.py | ||
from django import models | ||
from django_ltree import TreeModel | ||
class CustomTree(TreeModel): | ||
text = models.TextField() | ||
Create a child without parent | ||
----------------------------- | ||
|
||
.. code-block:: python | ||
from .models import CustomTree | ||
CustomTree.objects.create_child(text='Hello world') | ||
The following code with create a child with no parent (ie:roots of a tree) | ||
|
||
Create a child with parent | ||
-------------------------- | ||
Let's assume we want to add a child to the CustomTree object of `pk 1` | ||
|
||
.. code-block:: python | ||
from .models import CustomTree | ||
# This must return a single object | ||
parent: CustomTree = CustomTree.objects.get(pk=1) | ||
CustomTree.objects.create_child(text='Hello world', parent=parent) | ||
Get all the roots of the model | ||
------------------------------ | ||
A root means the the object that childrens anchor to. | ||
|
||
.. code-block:: python | ||
from .models import CustomTree | ||
roots: list[CustomTree] = CustomTree.objects.roots() | ||
Get all the childrens of a object | ||
--------------------------------- | ||
To get the childrens of a object, we can first get the object then call the QuerySet specific children function. | ||
|
||
.. code-block:: python | ||
from .models import CustomTree | ||
instance = CustomTree.objects.get(pk=1) | ||
childrens: list[CustomTree] = instance.children() | ||
Get the length of childrens of a object | ||
--------------------------------------- | ||
`django` specific database functions still work when we call the filter method. | ||
|
||
Lets assume we want to get the childrens of `CustomTree` object whose pk is 1. | ||
|
||
.. code-block:: python | ||
from .models import CustomTree | ||
instance = CustomTree.objects.get(pk=1) | ||
childrens: int = instance.children().count() | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters