MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
APIView & ViewSet0%
Study Materials
Django for Backend DevelopmentProgress 0%
1.1Installing Django & Environment Setup1.2Django Project Structure1.3Creating First Django App
6.1Serializers6.2APIView & ViewSet6.3Routing APIs with DRF
HomeTutorialsDjango for Backend DevelopmentAPIView & ViewSet
Routing APIs with DRF

APIView & ViewSet

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

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

1Level 1: @api_view decorator (Functional) - Explicit, simple
2Level 2: APIView (Class-based) - Granular control over GET, POST, PUT, DELETE
3Level 3: Generic Views (ListCreateAPIView, RetrieveUpdateDestroyAPIView) - Fast boilerplate-free CRUD
4Level 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:

1# articles/views.py
2from rest_framework.views import APIView
3from rest_framework.response import Response
4from rest_framework import status
5from rest_framework.permissions import IsAuthenticated
6from .models import Article
7from .serializers import ArticleSerializer
8 
9class ArticleListAPIView(APIView):
10 # Enforce authentication
11 permission_classes = [IsAuthenticated]
12 
13 def get(self, request):
14 articles = Article.objects.filter(status="published")
15 serializer = ArticleSerializer(articles, many=True)
16 return Response(serializer.data, status=status.HTTP_200_OK)
17 
18 def post(self, request):
19 serializer = ArticleSerializer(data=request.data)
20 if serializer.is_valid():
21 serializer.save(author=request.user)
22 return Response(serializer.data, status=status.HTTP_201_CREATED)
23 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:

1from rest_framework.generics import ListCreateAPIView, RetrieveUpdateDestroyAPIView
2 
3class ArticleListCreateView(ListCreateAPIView):
4 queryset = Article.objects.all()
5 serializer_class = ArticleSerializer
6 permission_classes = [IsAuthenticated]
7 
8 def perform_create(self, serializer):
9 serializer.save(author=self.request.user)
10 
11class ArticleDetailView(RetrieveUpdateDestroyAPIView):
12 queryset = Article.objects.all()
13 serializer_class = ArticleSerializer
14 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}/
1from rest_framework.viewsets import ModelViewSet
2from rest_framework.decorators import action
3from .models import Article
4from .serializers import ArticleSerializer
5 
6class ArticleViewSet(ModelViewSet):
7 queryset = Article.objects.all()
8 serializer_class = ArticleSerializer
9 lookup_field = "slug"
10 
11 # Custom non-standard action: POST /articles/{slug}/publish/
12 @action(detail=True, methods=["post"], url_path="publish")
13 def publish_article(self, request, slug=None):
14 article = self.get_object()
15 article.status = "published"
16 article.save()
17 return Response({"status": "Article marked as published"})

Interactive Quiz• Knowledge Check

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.

Mentor-Led Practical Training

Master Django for Backend Development with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext