{"id":23607,"library":"django-treenode","title":"django-treenode","description":"Probably the best abstract model/admin for your tree based stuff. Provides a Django model and admin mixin for managing tree structures using Materialized Path. Current version 0.24.0 supports Django 3.2–6.0 and Python 3.7–3.14. Released regularly, with recent versions adding Django 6.0 and Python 3.14 support.","status":"active","version":"0.24.0","language":"python","source_language":"en","source_url":"https://github.com/fabiocaccamo/django-treenode","tags":["django","tree","materialized-path","model","admin","hierarchy"],"install":[{"cmd":"pip install django-treenode","lang":"bash","label":"install from PyPI"}],"dependencies":[{"reason":"core dependency","package":"Django","optional":false},{"reason":"used for admin tree view assets","package":"django-js-asset","optional":true}],"imports":[{"note":"TreeNode is not the model; the base class is TreeNodeModel","wrong":"from treenode import TreeNode","symbol":"TreeNodeModel","correct":"from treenode.models import TreeNodeModel"}],"quickstart":{"code":"from django.db import models\nfrom treenode.models import TreeNodeModel\n\nclass Category(TreeNodeModel):\n    tn_parent = models.ForeignKey(\n        'self',\n        on_delete=models.CASCADE,\n        null=True,\n        blank=True,\n        related_name='children'\n    )\n    name = models.CharField(max_length=255)\n\n    def __str__(self):\n        return self.name","lang":"python","description":"Define a tree model by subclassing TreeNodeModel and adding a tn_parent ForeignKey. The model automatically gets fields like tn_priority, tn_order, tn_level, tn_count, etc."},"warnings":[{"fix":"Add a ForeignKey to self with related_name='children' and null=True, blank=True, and name it exactly 'tn_parent'.","message":"You must define a ForeignKey named 'tn_parent' on your model. Using a different name will not be recognized.","severity":"gotcha","affected_versions":"all"},{"fix":"Ensure Django's cache framework is configured (e.g., CACHES setting). If you don't need caching, set a dummy cache.","message":"Cache must be configured for tree operations to work correctly. If not, tree methods may silently fail or log warnings.","severity":"gotcha","affected_versions":">=0.22.0"},{"fix":"Upgrade to 0.23.3 or later.","message":"Circular references are now detected (since 0.23.3) but can still cause infinite recursion in older versions. Upgrade if you see recursion errors.","severity":"gotcha","affected_versions":"<0.23.3"},{"fix":"Upgrade to 0.20.2 or later.","message":"In multi-database setups, write operations may not use the correct database. Fixed in 0.20.2.","severity":"gotcha","affected_versions":"<0.20.2"}],"env_vars":null,"last_verified":"2026-05-01T00:00:00.000Z","next_check":"2026-07-30T00:00:00.000Z","problems":[{"fix":"Ensure your model inherits from TreeNodeModel and has a ForeignKey called 'tn_parent'.","cause":"The model does not inherit from TreeNodeModel or the ForeignKey is not named 'tn_parent'.","error":"django.core.exceptions.ImproperlyConfigured: The model 'myapp.Category' is not a TreeNodeModel."},{"fix":"Upgrade to >=0.23.3, or check your code for circular parent assignments.","cause":"Circular parent assignment detected (or a bug in versions <0.23.3).","error":"RuntimeError: maximum recursion depth exceeded"},{"fix":"Ensure the model inherits from TreeNodeModel and that you have run migrations after adding the inheritance.","cause":"The model fields are not created because the model was not properly set up as a TreeNodeModel.","error":"AttributeError: 'Category' object has no attribute 'tn_order'"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}