Hybrid Tree Storage
Django Fast TreeNode is a high-performance Django application for working with tree structures.
- Hybrid storage model: Combines Adjacency List and Closure Table for optimal performance.
- Custom caching system: A built-in caching mechanism, specifically designed for this package, significantly boosts execution speed.
- Efficient queries: Retrieve ancestors, descendants, breadcrumbs, and tree depth with only one SQL queriy.
- Bulk operations: Supports fast insertion, movement, and deletion of nodes.
- Flexibility: Fully integrates with Django ORM and adapts to various business logic needs.
- Admin panel integration: Full compatibility with Django's admin panel, allowing intuitive management of tree structures.
- Import & Export functionality: Built-in support for importing and exporting tree structures in multiple formats (CSV, JSON, XLSX, YAML, TSV), including integration with the Django admin panel.
It seems that django-fast-treenode is currently the most balanced and performant solution for most tasks, especially those related to dynamic hierarchical data structures. Check out the results of (comparison tests)[#] with other Django packages.
Django Fast TreeNode is suitable for a wide range of applications, from simple directories to complex systems with deep hierarchical structures:
- Categories and taxonomies: Manage product categories, tags, and classification systems.
- Menus and navigation: Create tree-like menus and nested navigation structures.
- Forums and comments: Store threaded discussions and nested comment chains.
- Geographical data: Represent administrative divisions, regions, and areas of influence.
- Organizational and Business Structures: Model company hierarchies, business processes, employees and departments.
- Run
pip install django-fast-treenode
. - Add
treenode
tosettings.INSTALLED_APPS
.
INSTALLED_APPS = [
...
'treenode',
]
- Define your model inherit from
treenode.models.TreeNodeModel
.
from treenode.models import TreeNodeModel
class Category(TreeNodeModel):
name = models.CharField(max_length=255)
treenode_display_field = "name"
- Make your model-admin inherit from
treenode.admin.TreeNodeModelAdmin
.
from treenode.admin import TreeNodeModelAdmin
from .models import Category
@admin.register(Category)
class CategoryAdmin(TreeNodeModelAdmin):
list_display = ("name",)
search_fields = ("name",)
- Run migrations.
python manage.py makemigrations
python manage.py migrate
- Run server and use!
>>> root = Category.objects.create(name="Root")
>>> child = Category.objects.create(name="Child")
>>> child.set_parent(root)
>>> root_descendants_list = root.get_descendants()
>>> root_children_queryset = root.get_children_queryset()
>>> ancestors_pks = child.get_ancestors_pks()
Full documentation is available at ReadTheDocs.
Quick access links:
- Installation, configuration and fine tuning
- Model Inheritance and Extensions
- Working with Admin Classes
- API Reference
- Import & Export
- Caching and working with cache
- Migration and upgrade guide
Your wishes, objections, comments are welcome.
Released under MIT License.
Thanks to everyone who contributed to the development and testing of this package, as well as the Django community for their inspiration and support.
Special thanks to Fabio Caccamo for the idea behind creating a fast Django application for handling hierarchies.