drf-nested-routers
Routers and relation fields for building nested REST resources in Django REST Framework.
Repository Health
Technical Analysis
drf-nested-routers extends Django REST Framework with routers and serializer fields for modeling nested resources — resources that only make sense inside a parent, like nameservers within a domain or comments within a post. It generates the full nested URL structure (e.g. /domains/{domain_pk}/nameservers/{pk}/) so your API mirrors the real hierarchy of your data.
Alongside the nested routers, it ships hyperlinked relation fields and serializers that understand the parent lookup keys, so links and lookups across the nesting resolve correctly with minimal boilerplate.
What You Get
NestedSimpleRouter/NestedDefaultRouterfor registering child viewsets under a parent route- Automatic parent lookup kwargs (e.g.
domain_pk) passed through to nested viewsets NestedHyperlinkedRelatedFieldandNestedHyperlinkedIdentityFieldfor correct nested links- Serializer mixins that resolve nested hyperlinked relationships
Common Use Cases
- Exposing child resources under their parent’s URL, like
/domains/{id}/nameservers/ - Building fully RESTful URL hierarchies for one-to-many relationships
- Generating correct hyperlinked representations for nested resources
Under The Hood
Architecture - The package lives in the rest_framework_nested module and layers on top of DRF’s own routers. routers.py defines NestedMixin and the NestedSimpleRouter/NestedDefaultRouter classes, which take a parent router, a parent prefix, and a lookup name, then rewrite child URL patterns to include the parent’s captured kwarg. relations.py and serializers.py provide hyperlinked fields and serializer bases that read those parent kwargs from the request context to build and reverse nested URLs, while viewsets.py supplies helpers for accessing the parent lookup inside a child viewset.
Tech Stack - Pure Python (ships py.typed for type checkers), depending on Django and Django REST Framework. It supports a broad compatibility matrix — Python 3.9–3.13, Django 4.2–5.2, and DRF 3.14–3.16 — validated in CI across combinations.
Code Quality - The repository has a thorough test suite (test_routers.py, test_dynamic_routers.py, test_viewsets.py, plus serializer tests) run under a dedicated runtests harness with coverage, and CI exercises the full version matrix. With 1,797 stars, 63 contributors, and 500+ commits over more than a decade, it is a well-established DRF extension.
API Design - The API deliberately mirrors DRF’s native router registration, so developers already comfortable with DefaultRouter can adopt nesting with only an extra parent-lookup argument. The naming (Nested* variants of familiar DRF classes) makes the mapping obvious, keeping the learning curve low for the DRF audience it targets.