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
Create, Retrieve, Update, Delete APIs0%
Study Materials
REST API Development with DjangoProgress 0%
1.1REST Principles1.2HTTP Methods Overview1.3Status Codes
3.1Create, Retrieve, Update, Delete APIs3.2Endpoints for Models3.3URL Routing
HomeTutorialsREST API Development with DjangoCreate, Retrieve, Update, Delete APIs
Endpoints for Models

Create, Retrieve, Update, Delete APIs

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
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.
1# articles/views.py
2from rest_framework.generics import ListCreateAPIView, RetrieveUpdateDestroyAPIView
3from rest_framework.permissions import IsAuthenticatedOrReadOnly
4from .models import Article
5from .serializers import ArticleSerializer
6 
7class ArticleListCreateAPIView(ListCreateAPIView):
8 queryset = Article.objects.select_related("author").all()
9 serializer_class = ArticleSerializer
10 permission_classes = [IsAuthenticatedOrReadOnly]
11 
12 def perform_create(self, serializer):
13 # Automatically attach author
14 serializer.save(author=self.request.user)
15 
16class ArticleDetailAPIView(RetrieveUpdateDestroyAPIView):
17 queryset = Article.objects.all()
18 serializer_class = ArticleSerializer
19 lookup_field = "slug"

2. The Complete CRUD Controller: ModelViewSet

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

1# articles/views.py
2from rest_framework.viewsets import ModelViewSet
3from rest_framework.permissions import IsAuthenticated
4from .models import Article
5from .serializers import ArticleSerializer
6 
7class ArticleViewSet(ModelViewSet):
8 queryset = Article.objects.select_related("author").all()
9 serializer_class = ArticleSerializer
10 permission_classes = [IsAuthenticated]
11 lookup_field = "slug"
12 
13 def perform_create(self, serializer):
14 serializer.save(author=self.request.user)
15 
16 def get_queryset(self):
17 # Dynamically customize queryset based on caller
18 if self.request.user.is_staff:
19 return Article.objects.all()
20 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():

1class ArticleViewSet(ModelViewSet):
2 queryset = Article.objects.all()
3 
4 def get_serializer_class(self):
5 if self.action == "list":
6 return ArticleSummarySerializer
7 elif self.action in ["create", "update"]:
8 return ArticleWriteSerializer
9 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• Knowledge Check

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

Previous LessonNext Lesson
Function-Based Views & Class-Based ViewsEndpoints for Models

Practice Quiz

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

Mentor-Led Practical Training

Master REST API Development with Django 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