Building enterprise APIs often involves writing identical CRUD endpoints across dozens of models. Writing raw APIView classes for every table introduces repetitive boilerplate. Django REST Framework solves this through Generic Views and ModelViewSet, delivering complete CRUD operations with minimal code.
DRF pairs GenericAPIView with CRUD mixins to provide ready-to-use concrete generic views:
| Generic View | Allowed Methods | Purpose |
|---|---|---|
ListAPIView | GET | Read a collection of resources. |
CreateAPIView | POST | Create a new resource. |
ListCreateAPIView | GET, POST | Read collection and create new items. |
RetrieveAPIView | GET | Read single resource by lookup field. |
UpdateAPIView | PUT, PATCH | Update single resource. |
DestroyAPIView | DELETE | Delete single resource. |
RetrieveUpdateDestroyAPIView | GET, PUT, PATCH, DELETE | Complete single-resource management. |
ModelViewSetWhy write two separate generic views when a single ModelViewSet handles everything?
In enterprise applications, list views often need summary fields, while retrieve and create views require detailed or write-only fields. Override get_serializer_class():
Generic views and ViewSets provide dedicated hooks to execute business logic without hacking method overrides:
perform_create(serializer): Called before saving a new instance.perform_update(serializer): Called before updating an instance.perform_destroy(instance): Called before deleting an instance (ideal for soft deletes).Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Function-Based Views & Class-Based Views | Endpoints for Models |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.
Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.