Create, Retrieve, Update, Delete APIs0%

Create, Retrieve, Update, Delete APIs

Beginner12 min read•Updated: Jul 11, 2026
Study MaterialsCheatsheet

DRF Generic Views and ModelViewSet CRUD

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.


1. DRF's Concrete Generic Views

DRF pairs GenericAPIView with CRUD mixins to provide ready-to-use concrete generic views:

Generic ViewAllowed MethodsPurpose
ListAPIViewGETRead a collection of resources.
CreateAPIViewPOSTCreate a new resource.
ListCreateAPIViewGET, POSTRead collection and create new items.
RetrieveAPIViewGETRead single resource by lookup field.
UpdateAPIViewPUT, PATCHUpdate single resource.
DestroyAPIViewDELETEDelete single resource.
RetrieveUpdateDestroyAPIViewGET, PUT, PATCH, DELETEComplete single-resource management.
Python
# articles/views.py
from rest_framework.generics import ListCreateAPIView, RetrieveUpdateDestroyAPIView
from rest_framework.permissions import IsAuthenticatedOrReadOnly
from .models import Article
from .serializers import ArticleSerializer
 
class ArticleListCreateAPIView(ListCreateAPIView):
queryset = Article.objects.select_related("author").all()
serializer_class = ArticleSerializer
permission_classes = [IsAuthenticatedOrReadOnly]
 
def perform_create(self, serializer):
# Automatically attach author
serializer.save(author=self.request.user)
 
class ArticleDetailAPIView(RetrieveUpdateDestroyAPIView):
queryset = Article.objects.all()
serializer_class = ArticleSerializer
lookup_field = "slug"

2. The Complete CRUD Controller: ModelViewSet

Why write two separate generic views when a single ModelViewSet handles everything?

Python
# articles/views.py
from rest_framework.viewsets import ModelViewSet
from rest_framework.permissions import IsAuthenticated
from .models import Article
from .serializers import ArticleSerializer
 
class ArticleViewSet(ModelViewSet):
queryset = Article.objects.select_related("author").all()
serializer_class = ArticleSerializer
permission_classes = [IsAuthenticated]
lookup_field = "slug"
 
def perform_create(self, serializer):
serializer.save(author=self.request.user)
 
def get_queryset(self):
# Dynamically customize queryset based on caller
if self.request.user.is_staff:
return Article.objects.all()
return Article.objects.filter(status="published")

3. Dynamic Serializers per Action

In enterprise applications, list views often need summary fields, while retrieve and create views require detailed or write-only fields. Override get_serializer_class():

Python
class ArticleViewSet(ModelViewSet):
queryset = Article.objects.all()
 
def get_serializer_class(self):
if self.action == "list":
return ArticleSummarySerializer
elif self.action in ["create", "update"]:
return ArticleWriteSerializer
return ArticleDetailSerializer

4. Lifecycle Hooks for Business Logic

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).

Interactive Quiz

Practice Quiz

Question 1
What concrete generic view combines listing all items (GET) with creating a new item (POST)?
Question 2
What is the primary difference between ListCreateAPIView and a ModelViewSet?
Question 3
How do you serve different serializers for list vs detail actions in a ModelViewSet?
Question 4
Which lifecycle hook should you override to implement "soft deletion" (marking is_deleted=True instead of executing SQL DELETE)?
Question 5
What does setting lookup_field = "slug" on a ModelViewSet accomplish?
Next Lesson

Endpoints for Models

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

PrevNext