0
0
Djangoframework~10 mins

Generic views in DRF in Django - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Generic views in DRF
Request comes in
Generic View Class
Dispatch method
Determine HTTP method
GET
ListModelMixin or RetrieveModelMixin
POST
CreateModelMixin
PUT/PATCH
UpdateModelMixin
DELETE
DestroyModelMixin
Perform action
Serialize data
Return Response
The generic view receives a request, decides which HTTP method is called, uses the matching mixin to handle the action, serializes data, and sends back a response.
Execution Sample
Django
from rest_framework import viewsets

class BookViewSet(viewsets.ModelViewSet):
    queryset = Book.objects.all()
    serializer_class = BookSerializer
This code creates a view that lists all books on GET and creates a new book on POST.
Execution Table
StepHTTP MethodAction SelectedQueryset AccessedSerializer UsedResponse Type
1GETListModelMixin.list()Book.objects.all()BookSerializerList of books JSON
2POSTCreateModelMixin.create()Book.objects.all()BookSerializerCreated book JSON
3PUTUpdateModelMixin.update()Book.objects.all()BookSerializerUpdated book JSON
4DELETEDestroyModelMixin.destroy()Book.objects.all()BookSerializer204 No Content
5PATCHUpdateModelMixin.partial_update()Book.objects.all()BookSerializerPartially updated book JSON
6OPTIONSGenericAPIView.options()N/AN/AAllowed methods info
💡 Request handled by matching mixin method based on HTTP method, response returned.
Variable Tracker
VariableStartAfter GETAfter POSTAfter PUTAfter DELETEAfter PATCH
request.methodNoneGETPOSTPUTDELETEPATCH
actionNonelist()create()update()destroy()partial_update()
querysetNoneBook.objects.all()Book.objects.all()Book.objects.all()Book.objects.all()Book.objects.all()
serializerNoneBookSerializerBookSerializerBookSerializerBookSerializerBookSerializer
response.status_codeNone200 OK201 Created200 OK204 No Content200 OK
Key Moments - 3 Insights
Why does the same queryset get used for all HTTP methods?
The queryset is defined once in the generic view and reused by all mixin methods to access data consistently, as shown in execution_table rows 1-5.
How does the view know which method to call for each HTTP request?
The dispatch method checks request.method and calls the corresponding mixin method, as seen in the concept_flow and execution_table where each HTTP method maps to a specific action.
Why is the serializer class the same for all actions?
The serializer_class is set once in the view and used to convert data to/from JSON for all actions, ensuring consistent data format, as tracked in variable_tracker.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what response status code is returned after a DELETE request?
A201 Created
B204 No Content
C200 OK
D404 Not Found
💡 Hint
Check the response.status_code column for the DELETE row in execution_table.
At which step does the view use the CreateModelMixin?
AStep 2
BStep 1
CStep 4
DStep 5
💡 Hint
Look at the Action Selected column in execution_table for the POST method.
If the serializer_class was changed, which variable_tracker row would be affected?
Arequest.method
Baction
Cserializer
Dqueryset
💡 Hint
See which variable holds the serializer class in variable_tracker.
Concept Snapshot
Generic views in DRF simplify API views by combining mixins.
They handle common actions: list, create, retrieve, update, destroy.
You define queryset and serializer_class once.
HTTP methods map to mixin methods automatically.
This reduces code and keeps views clean.
Full Transcript
Generic views in Django REST Framework help you write less code by providing ready-made classes for common API actions. When a request comes in, the generic view checks the HTTP method like GET or POST. It then calls the matching mixin method such as list() for GET or create() for POST. The view uses the queryset you set to get data and the serializer_class to convert data to JSON. Finally, it sends back the response. This flow makes building APIs faster and easier because you don't write repetitive code for each action.