Architecture and Design
This page explains how the package works beyond the API surface and why the current module split exists.
Design goals
Keep serializer APIs close to native DRF behavior.
Make output-shape control request-driven and explicit.
Allow opt-in queryset optimization without hidden magic.
Module responsibilities
serializers.pyImplements expansion, sparse fieldsets, and nested option propagation via
FlexFieldsSerializerMixinandFlexFieldsModelSerializer.views.pyProvides
FlexFieldsMixinandFlexFieldsModelViewSetto control list action expansion and pass serializer context constraints.filter_backends.pyProvides
FlexFieldsFilterBackendfor queryset optimization andFlexFieldsDocsFilterBackendfor schema parameter support.config.pyCentralizes runtime settings from
REST_FLEX_FIELDS2and exports constants used by serializers and utilities.utils.pyContains helper logic for parsing nested field paths and checking inclusion and expansion intent.
Serialization flow
Root serializer reads request parameters (or constructor kwargs).
Sparse field controls are resolved.
Expansion paths are validated for recursion and depth.
Nested serializers are constructed with propagated options.
Output payload is returned using resolved fields and expansions.
Key implementation choices
Mixin-first design supports custom serializer base classes.
Lazy serializer references support circular import scenarios.
Expansion limits and recursive policies are configurable globally and per serializer class.
Query optimization is optional and intentionally conservative.
Extension points
Customize query parameter names in
REST_FLEX_FIELDS.Override serializer validation hooks for expansion policy errors.
Restrict list expansions with
permit_list_expandson viewsets.Pair expansion rules with explicit
select_relatedandprefetch_relatedfor predictable performance.
When changing internals
Add tests for nested expansion and sparse field interactions.
Update user documentation if behavior changes.
Update this page if module responsibilities or flow changes.