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`.
pip install djangorestframework-recursiveVerified import paths — ran on the pinned version, not inferred.
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.
Consider using the actively maintained fork, `drf-recursive` (installable via `pip install drf-recursive`), which explicitly supports modern Python, Django, and DRF versions.
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()`).
Migrate your project to Python 3. If you need recursive serialization on Python 3, use the `drf-recursive` fork.
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.
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).
For one-to-many or many-to-many recursive relationships, ensure `RecursiveField` is used as the `child` of a `serializers.ListField` or `serializers.ManyRelatedField`.
No resource links recorded.