Discover how to stop repeating yourself and build APIs faster with less code!
Why Generic views in DRF in Django? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine building an API where you write separate code for listing, creating, updating, and deleting items for every model.
You have to repeat similar code again and again for each endpoint.
Writing all these views manually is slow and boring.
It's easy to make mistakes or forget to handle some HTTP methods properly.
Maintaining and updating this repeated code becomes a headache as your project grows.
Generic views in DRF provide ready-made classes that handle common API actions like list, create, update, and delete.
You just configure them with your model and serializer, and they do the heavy lifting for you.
class BookList(APIView): def get(self, request): books = Book.objects.all() serializer = BookSerializer(books, many=True) return Response(serializer.data)
class BookList(generics.ListAPIView):
queryset = Book.objects.all()
serializer_class = BookSerializerYou can build clean, consistent, and maintainable APIs quickly without rewriting common logic.
When creating a blog API, you can use generic views to handle posts and comments with minimal code, focusing on unique features instead of boilerplate.
Manual API views require repetitive code for common actions.
Generic views provide reusable classes for standard API operations.
This saves time, reduces errors, and makes your code easier to maintain.
Practice
Solution
Step 1: Understand generic views role
Generic views in DRF offer pre-built classes to handle common API tasks such as listing, creating, updating, and deleting data.Step 2: Compare options
Options B, C, and D describe unrelated tasks: raw SQL, manual auth, and frontend templates, which are not the main purpose of generic views.Final Answer:
To provide ready-made classes that simplify common API tasks like CRUD operations -> Option AQuick Check:
Generic views simplify CRUD = A [OK]
- Confusing generic views with authentication classes
- Thinking generic views handle frontend rendering
- Assuming generic views require manual SQL
Book in DRF?Solution
Step 1: Identify the generic view for listing objects
TheListAPIViewis designed to list all objects of a model.Step 2: Match the class with the task
Options B, C, and D correspond to creating, updating, and deleting respectively, which do not list objects.Final Answer:
class BookList(generics.ListAPIView):\n queryset = Book.objects.all()\n serializer_class = BookSerializer -> Option AQuick Check:
List all objects = ListAPIView = A [OK]
- Using CreateAPIView for listing data
- Confusing UpdateAPIView with ListAPIView
- Forgetting to set queryset or serializer_class
class ArticleDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = Article.objects.all()
serializer_class = ArticleSerializerSolution
Step 1: Understand RetrieveUpdateDestroyAPIView
This generic view supports retrieving a single object (GET), updating it (PUT/PATCH), and deleting it (DELETE).Step 2: Match HTTP methods to actions
Supports GET, PUT, PATCH, DELETE methods to retrieve, update, or delete an article correctly describes the supported methods (GET for retrieve, PUT/PATCH for update, DELETE for destroy) for a single article. Options A, C, and D describe incorrect methods or actions.Final Answer:
Supports GET, PUT, PATCH, DELETE methods to retrieve, update, or delete an article -> Option BQuick Check:
RetrieveUpdateDestroyAPIView = GET, PUT/PATCH, DELETE [OK]
- Thinking it supports POST for creation
- Confusing list view with detail view
- Assuming it deletes all objects instead of one
class UserCreate(generics.CreateAPIView):
serializer_class = UserSerializerSolution
Step 1: Check required attributes for CreateAPIView
While CreateAPIView requires serializer_class, it also typically needs a queryset attribute to function properly without errors, especially in standard setups with permissions.Step 2: Identify the error
The missing queryset attribute causes runtime errors in many scenarios. Options A, B, and D are incorrect: CreateAPIView does exist (A), serializer_class is the correct name (B), and class names should use PascalCase (D).Final Answer:
Missing queryset attribute causes runtime error -> Option DQuick Check:
Missing queryset = runtime error [OK]
- Omitting queryset attribute
- Renaming serializer_class incorrectly
- Thinking CreateAPIView is invalid
- Using lowercase class names
Product items and creating new ones in the same view. Which generic view class should you use and why?Solution
Step 1: Identify the requirement
The endpoint must list all products and allow creating new ones in the same view.Step 2: Match generic view to requirement
ListCreateAPIViewis 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.Final Answer:
Use generics.ListCreateAPIView because it supports both listing and creating in one view -> Option CQuick Check:
List + Create = ListCreateAPIView [OK]
- Using RetrieveUpdateDestroyAPIView for list/create
- Choosing only CreateAPIView or ListAPIView alone
- Not combining actions in one view
