django-rest-framework-recursive

Recursive serialization for Django REST Framework via a single reusable RecursiveField.

Library
PyPI
v0.1.2
366stars
ISC

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
34/100Needs Attention
Development Activity0
Maintenance0
Community56
Maturity60
Momentum20

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
77/100Good
Architecture78
Code Quality72
Innovation74
Learning Curve85

django-rest-framework-recursive provides a RecursiveField that lets you serialize self-referential data structures such as trees, linked lists, and directed acyclic graphs with Django REST Framework. Instead of manually declaring nested serializers, you reference the parent serializer recursively, and the field proxies representation, validation, and deserialization back to it.

The package integrates cleanly with plain Serializers and ModelSerializers, supports validation and deserialization as well as read-only output, and can point to another serializer by name or import path for multi-step recursive structures.

What You Get

  • A single RecursiveField class that plugs into any DRF Serializer or ModelSerializer
  • Support for serializing trees, linked lists, and directed acyclic graphs
  • Bidirectional support for validation and deserialization, not just read-only output
  • The ability to target another serializer by name or fully qualified import path for multi-step recursion

Common Use Cases

  • Serializing category or comment trees with arbitrarily deep children
  • Representing linked-list style next/previous chains in an API response
  • Modeling directed acyclic graphs where nodes reference other nodes of the same type

Under The Hood

Architecture - The entire package is a single module, rest_framework_recursive/fields.py, defining one RecursiveField class that subclasses DRF’s Field. Rather than serialize data itself, it lazily binds to its parent (capturing field_name and parent in bind) and, on first access via the proxied property, resolves the correct serializer class: the parent serializer’s class when to is None, or a named/dotted-path serializer otherwise. It then instantiates and binds that serializer and forwards a fixed set of PROXIED_ATTRS (get_value, run_validation, to_representation, source, read_only, and so on) through a custom getattribute, so DRF treats the field as if it were the target serializer.

Tech Stack - Pure Python with no dependencies beyond Django and djangorestframework (>= 3.0). It uses the standard library inspect and importlib for signature introspection and dynamic module loading, and packages via setup.py. CI historically ran through Travis and tox against a matrix of Python and Django/DRF versions.

Code Quality - The code is compact, well-commented, and defensively handles the tricky ListField-nesting case where the RecursiveField is bound before its container. A dedicated tests/ package with test_recursive.py exercises tree and linked-list serialization. The library relies on getattribute interception, which is clever but subtle; the small surface area keeps this manageable.

API Design - The public API is a single field you drop into an existing serializer, e.g. children = ListField(child=RecursiveField()) or next = RecursiveField(allow_null=True). It requires almost no boilerplate and reads naturally in DRF serializer definitions. The optional to argument for cross-serializer recursion is the only real concept to learn, making the learning curve shallow for anyone already comfortable with DRF.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search