Skip to content

TimurKady/django-fast-treenode

Repository files navigation

Django-fast-treenode

Hybrid Tree Storage

Tests Docs PyPI Published on Django Packages Sponsor

Django Fast TreeNode is a high-performance Django application for working with tree structures.

Features

  • 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.

Use Cases

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.

Quick start

  1. Run pip install django-fast-treenode.
  2. Add treenode to settings.INSTALLED_APPS.
INSTALLED_APPS = [
    ...
    'treenode',
]
  1. 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"
  1. 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",)
  1. Run migrations.
python manage.py makemigrations
python manage.py migrate
  1. 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()

Documentation

Full documentation is available at ReadTheDocs.

Quick access links:

Your wishes, objections, comments are welcome.

License

Released under MIT License.

Credits

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.