django_trees
Simple way to create, persist and manipulate reliable tree structures using Django models.
Installation
pip install django-trees
API Usage
Create Model
To create a model extend AbstractNode and add desired fields.
from django.db import models
from django_trees.models import AbstractNode
class Folder(AbstractNode):
name = models.CharField(max_length=10)
Create Tree Nodes
To create a tree node, there is nothing different than creating a normal Django model other than you may specify a parent node.
root = Folder.objects.create(name="Root")
documents = Folder.objects.create(name="Documents", parent=root)
downloads = Folder.objects.create(name="Downloads", parent=root)
projects = Folder.objects.create(name="Projects", parent=documents)
Get Node Descendants
To retrieve all of the descendants of a node (including children, grandchildren, great grandchildren, etc) use the get_descendants method. This method will return a flat list of node objects.
root.get_descendants()
Get Node Ancestors
To retrieve all of the ancestors of a node (including parents, grandparents, great grandparents, etc) use the get_ancestors method. This method will return a flat list of node objects.
projects.get_ancestors()
Get Node Children
To retrieve all immediate children of the current node use the get_children method. This method will return a flat list of node objects.
projects.get_children()
Move Node
To move a node to a different position in the tree use the move method passing the new parent node as an argument.
projects.move(root)
Bifurcate Node
To create a separate tree from a branch of an existing tree use the bifurcate method. The node object will be removed from the previous tree and it along with its descendants will now be in a new tree.
projects.bifurcate()
Get ASCII Tree
To get an ascii representation of the tree structure use the get_ascii_tree method.
projects.get_ascii_tree()
Demo
Metadata
Release files for django_trees 2.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| django_trees-2.0.1.tar.gz | 9.4 kB | Without provenance | Details for django_trees-2.0.1.tar.gz |
Release files / django_trees-2.0.1.tar.gz
| Download URL | django_trees-2.0.1.tar.gz |
|---|---|
| Size | 9.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
954795d223cc7bf6f8218477b2256cbba2cab5bec452433738c01af21c677180
|
|
BLAKE2b-256 checksum How to use checksums |
2edbf9f7a3ca906a1af576c47f4a9e5e25199214f8a419a036133098d5b76e24
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for django_trees 2.0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.