A Django package that provides mixins for optimizing querysets in Django REST Framework views and viewsets.
- Defer specific fields in queryset using class attributes.
- Include only specific fields in queryset using class attributes.
pip install drf-queryset-optimization
Using the optimization mixins in your viewsets is straightforward:
-
Import the
QuerysetOptimizationMixin
fromdrf_queryset_optimization.mixins
. -
In your viewset class, inherit from
QuerysetOptimizationMixin
. -
Specify the fields you want to defer or include using the
defer_fields
andonly_fields
class attributes.
Here's a basic example of how you can use the package in your project:
from drf_queryset_optimization.mixins import QuerysetOptimizationMixin
from rest_framework.viewsets import ModelViewSet
from .models import YourModel
from .serializers import YourSerializer
class OptimizedModelViewSet(QuerysetOptimizationMixin, ModelViewSet):
defer_fields = ["field1", "field2"] # Fields to defer
only_fields = [] # Fields to include
queryset = YourModel.objects.all()
serializer_class = YourSerializer
MIT License