drf-flex-fields
Flexible, dynamic fields and nested resources for Django REST Framework serializers.
What it is and what it does
drf-flex-fields extends Django REST Framework serializers to let API clients dynamically control which fields appear in responses and which nested relationships get fully expanded. Instead of returning fixed response shapes, you define expandable fields in your serializer's Meta class, then clients use query parameters like `?expand=country.states` or `?fields=id,name` to customize what they receive. The package supports dot notation for arbitrarily deep nesting, sparse field selection (include or exclude specific fields), and can be configured per-request or passed directly to serializer instantiation.
The package has no runtime dependencies and integrates as a mixin or base class for your existing DRF serializers. It includes a ViewSet subclass to prevent expensive expansions on list endpoints and utility functions to check expansion state in view logic. The core appeal is reducing over-fetching and under-fetching in REST APIs without duplicating serializer definitions.
Use it for:
- Mobile apps that need lightweight responses with only essential fields to reduce bandwidth and latency
- Admin dashboards that request full nested details on demand without creating separate serializer variants
- GraphQL-like query control in REST APIs where clients specify exactly which relations to populate
- Preventing N+1 query problems by letting clients opt into expensive nested expansions rather than always including them
- Reusing a single serializer across multiple endpoints with different response shape requirements
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Adds dynamic field expansion and sparse field selection to Django REST Framework serializers, allowing API clients to request only the fields and nested resources they need via query parameters.
Yes, if you are already committed to Django REST Framework and need flexible field control without building custom logic. The permissive MIT license and zero runtime dependencies make adoption low-risk. However, the dormant maintenance status (last commit October 2023, no release for over a year) is a real concern—verify compatibility with your Django and DRF versions before relying on it for new projects, and be prepared to fork or patch if breaking changes emerge upstream.
Install
drf-flex-fields on PyPI
pip
pip install drf-flex-fieldsuv
uv add drf-flex-fieldspoetry
poetry add drf-flex-fieldsInstalling drf-flex-fields
Before you install
High install friction with no runtime dependencies. The package is dormant—last commit was 2023-10-16 and no release in over a year—so expect limited maintenance and potential compatibility drift with newer Django or DRF versions.
License in practice
MIT license permits free use, modification, and distribution with minimal restrictions, making it safe for commercial and open-source projects.
Quickstart
pip install drf-flex-fields
from rest_flex_fields import FlexFieldsModelSerializer
class CountrySerializer(FlexFieldsModelSerializer):
class Meta:
model = Country
fields = ('id', 'name', 'population', 'states')
expandable_fields = {'states': (StateSerializer, {'many': True})}
# GET /people/142/?expand=country.states
Verify before relying
- Compatibility with current Django and Django REST Framework versions (classifiers list Python up to 3.7 only)
- Whether the package works with modern async Django views or only synchronous serializers
- Performance impact of deep nested expansion on large datasets beyond the database query warnings in the description
Package facts
| License | MIT (permissive) |
| Python support | not specified |
| Install friction | high — source build required |
| Runtime dependencies | none |
| Maintenance | dormant — 1,252 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 215,044/month — #9,406 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: drf-flex-fields-1.0.2.tar.gz
Keywords: django, rest, api, dynamic, fields
Tags
More Dynamic Content packages
MarkupSafe provides a text object that escapes…
permissive · top 100 on PyPI
Jinja2Jinja2 is a templating engine that renders…
permissive · top 100 on PyPI
soupsieveSoupsieve is a CSS selector library designed to…
permissive · top 100 on PyPI
WerkzeugWerkzeug is a WSGI utility library providing…
permissive · top 1,000 on PyPI
FlaskFlask is a lightweight WSGI web application…
permissive · top 1,000 on PyPI
MakoMako compiles Python-embedded templates into…
permissive · top 1,000 on PyPI
drf-dynamic-fieldsProvides a Django REST Framework mixin that…
permissive · top 15,000 on PyPI
drf-writable-nestedExtends Django REST Framework serializers to…
permissive · top 15,000 on PyPI
drf-nested-routersProvides routers and serializer fields for…
permissive · top 5,000 on PyPI
djangorestframework-dataclassesAutomatically generates Django REST Framework…
permissive · top 5,000 on PyPI
rest-framework-generic-relationsProvides Django REST Framework serializers that…
permissive · top 15,000 on PyPI
djangorestframework-role-filtersAdds role-based access control to Django REST…
permissive · top 5,000 on PyPI
drf-rw-serializersExtends Django REST Framework with separate…
permissive · top 15,000 on PyPI
drf-extra-fieldsProvides specialized serializer fields for…
permissive · top 15,000 on PyPI
drf-jsonschema-serializerConverts Django REST Framework serializers to…
permissive · top 15,000 on PyPI
drf-access-policyDeclares access control rules for Django REST…
permissive · top 15,000 on PyPI