APIView & ViewSet0%

APIView & ViewSet

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

APIView & ViewSet

Django REST Framework provides layered abstractions for handling HTTP requests. From low-level, fully customized APIView classes to high-level automated ModelViewSet controllers, choosing the appropriate abstraction level accelerates development while preserving architectural flexibility.


1. DRF View Abstraction Levels

Output
Level 1: @api_view decorator (Functional) - Explicit, simple
Level 2: APIView (Class-based) - Granular control over GET, POST, PUT, DELETE
Level 3: Generic Views (ListCreateAPIView, RetrieveUpdateDestroyAPIView) - Fast boilerplate-free CRUD
Level 4: ViewSets (ModelViewSet) - Bundles list, create, retrieve, update, destroy into 1 class!

2. Granular Control with APIView

APIView subclasses Django's View class, adding request parsing, authentication policies, permission checks, and standard DRF Response objects:

Python
# articles/views.py
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from rest_framework.permissions import IsAuthenticated
from .models import Article
from .serializers import ArticleSerializer
 
class ArticleListAPIView(APIView):
# Enforce authentication
permission_classes = [IsAuthenticated]
 
def get(self, request):
articles = Article.objects.filter(status="published")
serializer = ArticleSerializer(articles, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)
 
def post(self, request):
serializer = ArticleSerializer(data=request.data)
if serializer.is_valid():
serializer.save(author=request.user)
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

3. High-Speed CRUD with Generic Views

When standard database operations are needed, DRF's generic views combine model querysets and serializers with zero boilerplate:

Python
from rest_framework.generics import ListCreateAPIView, RetrieveUpdateDestroyAPIView
 
class ArticleListCreateView(ListCreateAPIView):
queryset = Article.objects.all()
serializer_class = ArticleSerializer
permission_classes = [IsAuthenticated]
 
def perform_create(self, serializer):
serializer.save(author=self.request.user)
 
class ArticleDetailView(RetrieveUpdateDestroyAPIView):
queryset = Article.objects.all()
serializer_class = ArticleSerializer
lookup_field = "slug"

4. The Apex Abstraction: ModelViewSet

A ModelViewSet combines all five standard REST actions into a single class:

  • list(): GET /articles/
  • create(): POST /articles/
  • retrieve(): GET /articles/{id}/
  • update(): PUT /articles/{id}/
  • partial_update(): PATCH /articles/{id}/
  • destroy(): DELETE /articles/{id}/
Python
from rest_framework.viewsets import ModelViewSet
from rest_framework.decorators import action
from .models import Article
from .serializers import ArticleSerializer
 
class ArticleViewSet(ModelViewSet):
queryset = Article.objects.all()
serializer_class = ArticleSerializer
lookup_field = "slug"
 
# Custom non-standard action: POST /articles/{slug}/publish/
@action(detail=True, methods=["post"], url_path="publish")
def publish_article(self, request, slug=None):
article = self.get_object()
article.status = "published"
article.save()
return Response({"status": "Article marked as published"})

Interactive Quiz

Practice Quiz

Question 1
What five REST operations does a ModelViewSet provide automatically?
Question 2
What is the primary difference between Django's native HttpResponse and DRF's Response object?
Question 3
How do you add a custom endpoint like /articles/{id}/publish/ to a ViewSet?
Question 4
In generic views like ListCreateAPIView, how do you inject the current user as author during creation?
Question 5
What is the benefit of using APIView over a standard Django Function-Based View?
Next Lesson

Routing APIs with DRF

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

Related Lessons

Previous LessonNext Lesson
SerializersRouting APIs with DRF

Practice Quiz

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

PrevNext