{"id":14534,"library":"djangorestframework-recursive","title":"Recursive Serialization for Django REST framework","description":"djangorestframework-recursive is a Python library that provides a `RecursiveField` for Django REST Framework serializers, enabling the serialization of tree, linked list, or directed acyclic graph structures. Maintained by @heywbj, the last release (0.1.2) was on July 4, 2017. Due to a lack of recent maintenance, users requiring compatibility with modern Django and Django REST Framework versions are advised to consider the actively maintained fork, `drf-recursive`.","status":"abandoned","version":"0.1.2","language":"en","source_language":"en","source_url":"https://github.com/heywbj/django-rest-framework-recursive","tags":["django","rest-framework","serialization","recursive","tree","graph"],"install":[{"cmd":"pip install djangorestframework-recursive","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Core framework dependency for ORM and web application structure. Tested against 1.8, 1.9, 2.0.","package":"Django","optional":false},{"reason":"Provides the base serializer classes and functionality that djangorestframework-recursive extends. Tested against 3.3-3.7.","package":"djangorestframework","optional":false},{"reason":"The language runtime. Tested against 2.7, 3.4, 3.6.","package":"Python","optional":false}],"imports":[{"symbol":"RecursiveField","correct":"from rest_framework_recursive.fields import RecursiveField"}],"quickstart":{"code":"from rest_framework import serializers\nfrom rest_framework_recursive.fields import RecursiveField\n\n# Example Model (assuming you have a Django model like this)\n# class Category(models.Model):\n#     name = models.CharField(max_length=100)\n#     parent = models.ForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='children')\n\nclass CategorySerializer(serializers.Serializer):\n    name = serializers.CharField(max_length=100)\n    children = serializers.ListField(child=RecursiveField(), required=False)\n\n# Example usage with a dummy data structure\n# In a real application, you would pass Django model instances\ndata = {\n    'name': 'Root',\n    'children': [\n        {\n            'name': 'Child 1',\n            'children': [\n                {'name': 'Grandchild 1.1'},\n                {'name': 'Grandchild 1.2'}\n            ]\n        },\n        {'name': 'Child 2'}\n    ]\n}\n\nserializer = CategorySerializer(data=data)\nif serializer.is_valid():\n    print(serializer.data)\nelse:\n    print(serializer.errors)","lang":"python","description":"This quickstart demonstrates how to define a recursive serializer for a tree-like structure using `RecursiveField`. It assumes a conceptual `Category` model with a self-referential `parent` foreign key, allowing for nested children. The `RecursiveField` automatically handles the self-referencing serialization."},"warnings":[{"fix":"Consider using the actively maintained fork, `drf-recursive` (installable via `pip install drf-recursive`), which explicitly supports modern Python, Django, and DRF versions.","message":"The `djangorestframework-recursive` library is no longer actively maintained. Its last release was in 2017, leading to potential incompatibility issues with modern Python (3.7+), Django (2.1+), and Django REST Framework (3.8+) versions.","severity":"breaking","affected_versions":"0.1.2 and earlier"},{"fix":"Implement strategies to limit recursion depth, optimize database queries with `select_related` or `prefetch_related` for related objects, or consider alternative serialization approaches for extremely large graphs. Review Python's default recursion limit (`sys.getrecursionlimit()`).","message":"Deeply nested or very wide recursive data structures can lead to `RecursionError: maximum recursion depth exceeded` during serialization, or significant performance degradation.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Migrate your project to Python 3. If you need recursive serialization on Python 3, use the `drf-recursive` fork.","message":"The original library explicitly supported Python 2.7. Using it with Python 2.7 is highly discouraged as Python 2.7 reached its end-of-life in 2020.","severity":"deprecated","affected_versions":"0.1.2 and earlier"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Review your data structure for unintended cycles or extreme depth. Increase Python's recursion limit (`sys.setrecursionlimit(limit)`) if genuinely necessary, but be aware of performance implications. Consider flattening parts of the hierarchy or loading data in chunks.","cause":"Attempting to serialize an excessively deep recursive structure without a proper depth limit or hitting Python's default recursion limit.","error":"RecursionError: maximum recursion depth exceeded while calling a Python object"},{"fix":"Ensure `djangorestframework-recursive` is installed via `pip install djangorestframework-recursive`. If you intended to use the fork, install `drf-recursive` and import `RecursiveField` from `django_rest_framework_recursive.fields` (note the different package name).","cause":"The `djangorestframework-recursive` package is not installed, or the fork `drf-recursive` was installed and the import path is incorrect.","error":"ModuleNotFoundError: No module named 'rest_framework_recursive'"},{"fix":"For one-to-many or many-to-many recursive relationships, ensure `RecursiveField` is used as the `child` of a `serializers.ListField` or `serializers.ManyRelatedField`.","cause":"Incorrect usage of `RecursiveField` within a serializer, for example, passing it directly as a field for a `many=True` relationship without wrapping it in a `ListField` or similar.","error":"TypeError: 'RecursiveField' object is not iterable"}],"ecosystem":"pypi"}