{"id":44754,"library":"django-modeltree","title":"django-modeltree","description":"A Django library for representing hierarchical data (tree structures) using a materialized path approach. Version 1.0.0, low release cadence (last updated 2021).","status":"active","version":"1.0.0","language":"python","source_language":"en","source_url":"https://github.com/thomst/django-modeltree","tags":["django","tree","materialized-path","hierarchy"],"install":[{"cmd":"pip install django-modeltree","lang":"bash","label":"PyPI install"}],"dependencies":[{"reason":"Core requirement for model tree functionality","package":"Django","optional":false}],"imports":[{"note":"The package name uses underscore but import is 'modeltree'","wrong":"from django_modeltree import ModelTree","symbol":"ModelTree","correct":"from modeltree import ModelTree"},{"note":"TreeForeignKey is directly in modeltree, not a submodule","wrong":"from modeltree.fields import TreeForeignKey","symbol":"TreeForeignKey","correct":"from modeltree import TreeForeignKey"}],"quickstart":{"code":"from django.db import models\nfrom modeltree import ModelTree\n\nclass Category(ModelTree):\n    name = models.CharField(max_length=100)\n\n# Creating root and child\nroot = Category.objects.create(name='Root')\nchild = Category.objects.create(name='Child', parent=root)\nprint(root.get_children())  # [child]","lang":"python","description":"Define a tree model by inheriting from ModelTree, then use create() with parent= for hierarchy."},"warnings":[{"fix":"Ensure custom save() calls super().save() to maintain path integrity.","message":"ModelTree overrides save() – will break if you override save() in your subclass without calling super().","severity":"breaking","affected_versions":"all"},{"fix":"Use get_tree() to get hierarchical structure.","message":"The get_descendants() method returns a flat queryset, not a tree; misleading for beginners.","severity":"deprecated","affected_versions":"all"},{"fix":"Increase path_max_length in Meta if deep trees exceed default.","message":"ModelTree uses a char field 'path' for materialized path; max depth is limited by path length (default 255 chars).","severity":"gotcha","affected_versions":"all"},{"fix":"Override delete() or use .delete(keep_children=True) if partial removal needed.","message":"delete() method also deletes all descendants by default; no confirmation.","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"search_vec":"'1.0.0':19 '2021':25 'approach':17 'cadenc':22 'data':10 'django':2,5,26 'django-modeltre':1 'hierarch':9 'hierarchi':31 'last':23 'librari':6 'low':20 'materi':15,29 'materialized-path':28 'modeltre':3 'path':16,30 'releas':21 'repres':8 'structur':12 'tree':11,27 'updat':24 'use':13 'version':18","created_at":"2026-06-07T12:51:40.714349+00:00","updated_at":"2026-06-07T12:51:40.714349+00:00","problems":[{"fix":"Run python manage.py makemigrations and migrate after adding ModelTree subclass.","cause":"ModelTree's 'path' field is not defined until after first migration.","error":"AttributeError: 'ModelBase' object has no attribute 'path'"},{"fix":"Use TreeForeignKey (from modeltree import TreeForeignKey) for the parent field.","cause":"ModelTree uses a TreeForeignKey for parent but you tried to define parent as a plain ForeignKey.","error":"django.core.exceptions.FieldDoesNotExist: Category has no field named 'parent'"},{"fix":"Set path_max_length to a value <= 255 in Meta, or use a CharField path explicitly.","cause":"MySQL does not allow TEXT columns as primary keys or indexes without key length.","error":"OperationalError: (1170, \"BLOB/TEXT column 'path' used in key specification without a key length\")"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":null,"cli_version":null,"type":"library","homepage":"https://github.com/thomst/django-modeltree","github":"https://github.com/thomst/django-modeltree","docs":null,"changelog":null,"pypi":null,"npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["database"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-29","last_verified":"2026-06-29","next_check":"2026-07-29","install_tag":null}}