Performance: Generic views in DRF
This affects server response time and data serialization speed, impacting how fast the API sends data to the client.
Jump into concepts and practice - no test required
from rest_framework import generics class ItemList(generics.ListAPIView): queryset = Item.objects.all() serializer_class = ItemSerializer
from rest_framework.views import APIView from rest_framework.response import Response class ItemList(APIView): def get(self, request): items = Item.objects.all() serializer = ItemSerializer(items, many=True) return Response(serializer.data)
| Pattern | Database Queries | Serialization Calls | Response Time | Verdict |
|---|---|---|---|---|
| Manual APIView with custom get | 1 query per request | 1 serialization per request | Moderate | [!] OK |
| Generic ListAPIView | 1 optimized query per request | 1 serialization per request | Faster due to built-in optimizations | [OK] Good |
Book in DRF?ListAPIView is designed to list all objects of a model.class ArticleDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = Article.objects.all()
serializer_class = ArticleSerializerclass UserCreate(generics.CreateAPIView):
serializer_class = UserSerializerProduct items and creating new ones in the same view. Which generic view class should you use and why?ListCreateAPIView is the correct choice as it supports GET (listing) and POST (creating). Use generics.RetrieveUpdateDestroyAPIView because it handles all CRUD operations (RetrieveUpdateDestroyAPIView) is for single-object detail operations. Options A and C support only a single action each.