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
Custom Permissions0%
Study Materials
REST API Development with DjangoProgress 0%
1.1REST Principles1.2HTTP Methods Overview1.3Status Codes
4.1Token Authentication4.2JWT Authentication4.3Custom Permissions
HomeTutorialsREST API Development with DjangoCustom Permissions
Filtering API results

Custom Permissions

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

DRF Custom Permission Classes and Object-Level Permissions

Authentication identifies who a user is; permissions determine what that user is authorized to perform. While built-in classes (IsAuthenticated, AllowAny, IsAdminUser) handle basic access control, enterprise applications require fine-grained Custom Permission Classes and Object-Level Permissions (e.g. only the document author or organization admin can delete a file).


1. Built-in DRF Permission Classes

  • AllowAny: Unrestricted public access (e.g. user signup, public landing pages).
  • IsAuthenticated: Denies access to anonymous users.
  • IsAdminUser: Grants access strictly to staff users (user.is_staff == True).
  • IsAuthenticatedOrReadOnly: Authenticated users can mutate data; anonymous users are granted read-only access (GET, HEAD, OPTIONS).

2. Anatomy of a Custom Permission Class

A permission class inherits from rest_framework.permissions.BasePermission and implements one or both of these methods:

  1. 1
    has_permission(self, request, view): Evaluates view-level access (e.g. whether the user can access the /articles/ endpoint at all).
  2. 2
    has_object_permission(self, request, view, obj): Evaluates object-level access on a specific instance (e.g. whether the user can mutate Article #42).

3. Implementing IsOwnerOrReadOnly Object Permission

A classic enterprise permission: any user can read a document, but only the original author can edit or delete it:

1# api/permissions.py
2from rest_framework import permissions
3 
4class IsOwnerOrReadOnly(permissions.BasePermission):
5 '''
6 Object-level permission to only allow authors of an object to edit or delete it.
7 Assumes model instance has an `author` attribute.
8 '''
9 
10 def has_permission(self, request, view):
11 # View-level check: Allow all authenticated users
12 return request.user and request.user.is_authenticated
13 
14 def has_object_permission(self, request, view, obj):
15 # 1. Read permissions are allowed for any request (GET, HEAD, OPTIONS)
16 if request.method in permissions.SAFE_METHODS:
17 return True
18 
19 # 2. Write permissions are strictly restricted to the author or superuser
20 return obj.author == request.user or request.user.is_staff

4. Role-Based Organization Permission Class

1# api/permissions.py
2from rest_framework.permissions import BasePermission
3 
4class IsOrganizationAdmin(BasePermission):
5 message = "You must be an Organization Administrator to perform this action."
6 
7 def has_permission(self, request, view):
8 return (
9 request.user.is_authenticated
10 and getattr(request.user, "role", None) == "admin"
11 )

5. Applying Permissions to ViewSets

1# articles/views.py
2from rest_framework.viewsets import ModelViewSet
3from .models import Article
4from .serializers import ArticleSerializer
5from .permissions import IsOwnerOrReadOnly
6 
7class ArticleViewSet(ModelViewSet):
8 queryset = Article.objects.all()
9 serializer_class = ArticleSerializer
10 # Attach custom permission class
11 permission_classes = [IsOwnerOrReadOnly]
12 lookup_field = "slug"
13 
14 def perform_create(self, serializer):
15 serializer.save(author=self.request.user)

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
What is the difference between has_permission and has_object_permission in DRF?
Question 2
What HTTP methods are defined in permissions.SAFE_METHODS?
Question 3
When does DRF execute has_object_permission checks?
Question 4
How do you customize the error message returned when a custom permission class denies access?
Question 5
If a view defines permission_classes = [IsAuthenticated, IsAdminUser], what must be true for a request to succeed?
Next Lesson

Filtering API results

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

Related Lessons

Previous LessonNext Lesson
JWT AuthenticationFiltering API results

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