Registry / web-framework / drf-dynamic-fields

drf-dynamic-fields

JSON →
library0.4.0pypypiunverified

drf-dynamic-fields is a lightweight Django REST Framework utility that enables serializers to dynamically expose a subset of their fields based on query parameters in the API request. This can be useful for reducing payload size and tailoring responses for different client needs. The current version is 0.4.0, and releases are infrequent, reflecting its stable and focused scope.

pip install drf-dynamic-fields
INSTALL
IMPORT
SIG · DRF-DYNAMIC-FIELDS
D
drf-dynamic-fields
web-frameworkpythonv0.4.0
Install
1.6s avg
Import
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.4.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.000s · 17.8MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 1.6s · import 0.000s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

DynamicFieldsMixin
from drf_dynamic_fields import DynamicFieldsMixin
from drf_dynamic_fields import DynamicFieldsMixin

To use `drf-dynamic-fields`, inherit `DynamicFieldsMixin` in your Django REST Framework serializer. The mixin will automatically check for `fields` or `omit` query parameters in the request context and adjust the serializer's output accordingly. Ensure your view passes the request context to the serializer.

from rest_framework import serializers, viewsets from drf_dynamic_fields import DynamicFieldsMixin from django.db import models # --- Example Django Model --- class Product(models.Model): name = models.CharField(max_length=100) description = models.TextField() price = models.DecimalField(max_digits=10, decimal_places=2) is_available = models.BooleanField(default=True) def __str__(self): return self.name # --- DRF Serializer with DynamicFieldsMixin --- class ProductSerializer(DynamicFieldsMixin, serializers.ModelSerializer): class Meta: model = Product fields = ['id', 'name', 'description', 'price', 'is_available'] # --- DRF ViewSet --- class ProductViewSet(viewsets.ModelViewSet): queryset = Product.objects.all() serializer_class = ProductSerializer # To use this, configure Django URL patterns to include ProductViewSet. # Example API request with dynamic fields: # GET /api/products/?fields=id,name,price # GET /api/products/?omit=description,is_available
Debug
Known issues
gotchaThe `DynamicFieldsMixin` relies on the `request` object being present in the serializer's `context`. If you manually instantiate a serializer (e.g., in tests or custom logic) without providing `context={'request': request}`, dynamic field selection will not work and might raise a `KeyError`.
fix
Always ensure the serializer is initialized with `context={'request': request}`. DRF's `APIView` and `GenericAPIView` subclasses handle this automatically, but custom views or direct instantiations need explicit context passing.
affects: All versions
gotchaIf both `fields` and `omit` query parameters are provided, `drf-dynamic-fields` will prioritize `omit`. Fields listed in `omit` will always be removed, even if they are also listed in `fields`.
fix
Avoid providing both `fields` and `omit` query parameters in the same request to prevent ambiguity. If necessary, explicitly define a clear preference in your client-side logic.
affects: All versions
gotchaWhile `drf-dynamic-fields` effectively reduces the API response payload, it primarily affects the *serialization* stage. The underlying database query (ORM) might still fetch all columns for the model, leading to potential performance issues if your serializer includes many heavy fields or related objects that are then filtered out.
fix
For optimal performance, combine dynamic field selection with queryset optimizations like `only()` or `defer()` (for Django ORM) or `select_related()` / `prefetch_related()` when appropriate, to ensure your database queries only fetch necessary data.
affects: All versions
Upgrade
Version history
0.4.0latest on PyPI · released Apr 5, 2022
Audit
Dependencies
djangorestframeworkrequiredThis library extends Django REST Framework serializers and views.
Agent activity
21 hits · last 30 days
node
20
Resources
drf-dynamic-fields — pip install drf-dynamic-fields · libregistry