Permissions in Django REST Framework (DRF) control who can access your API. They help keep your data safe by allowing only the right users to do certain actions.
When you want to let only logged-in users see or change data.
When you want to allow only the owner of an object to edit it.
When you want to make some API endpoints public but others private.
When you want to restrict access based on user roles like admin or staff.
Syntax
Django
from rest_framework.permissions import BasePermission
class CustomPermission(BasePermission):
def has_permission(self, request, view):
# Return True if user has permissionreturnTrue# or Falsedef has_object_permission(self, request, view, obj):
# Return True if user has permission for this objectreturnTrue# or False
has_permission checks general access to the view.
has_object_permission checks access to a specific object.
Examples
This lets only users who are logged in access the API.
Django
from rest_framework.permissions import IsAuthenticated
# Use built-in permission to allow only logged-in users
permission_classes = [IsAuthenticated]
This custom permission allows only the owner of an object to access it.
Django
from rest_framework.permissions import BasePermission
class IsOwner(BasePermission):
def has_object_permission(self, request, view, obj):
return obj.owner == request.user
This makes the API open to everyone, no login needed.
Django
from rest_framework.permissions import AllowAny
# Allow anyone to access the API
permission_classes = [AllowAny]
Sample Program
This API view only lets logged-in users access it. When accessed, it greets the user by their username.
Django
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated
class HelloView(APIView):
permission_classes = [IsAuthenticated]
def get(self, request):
return Response({"message": f"Hello, {request.user.username}!"})
OutputSuccess
Important Notes
Always set permission_classes on your views to protect your API.
Use built-in permissions like IsAuthenticated before writing custom ones.
Test your permissions by trying to access the API as different users.
Summary
DRF permissions control who can use your API.
Use built-in permissions for common cases like logged-in users.
Create custom permissions for special rules like ownership.
Practice
(1/5)
1. What is the main purpose of permissions in Django REST Framework (DRF)?
easy
A. To control who can access or modify API endpoints
B. To style the API responses
C. To speed up database queries
D. To manage user sessions
Solution
Step 1: Understand the role of permissions in DRF
Permissions define rules about who can use or change API data.
Step 2: Compare options with permissions purpose
Only controlling access matches the purpose of permissions.
Final Answer:
To control who can access or modify API endpoints -> Option A
Quick Check:
Permissions = Access control [OK]
Hint: Permissions control access, not styling or speed [OK]
Common Mistakes:
Confusing permissions with styling or performance
Thinking permissions manage sessions
2. Which of the following is the correct way to apply the built-in permission IsAuthenticated to a DRF view?
easy
A. permissions = IsAuthenticated()
B. permission_classes = [IsAuthenticated]
C. permission_classes = IsAuthenticated
D. permission_classes = (IsAuthenticated)
Solution
Step 1: Recall DRF permission syntax
Permissions are set as a list or tuple in permission_classes.
Step 2: Check each option's syntax
permission_classes = [IsAuthenticated] uses a list with the class name, which is correct. permissions = IsAuthenticated() uses wrong attribute name and instance. permission_classes = IsAuthenticated misses list brackets. permission_classes = (IsAuthenticated) uses parentheses but without a comma, so it's not a tuple.
Final Answer:
permission_classes = [IsAuthenticated] -> Option B
Quick Check:
Use list for permission_classes [OK]
Hint: Use a list of permission classes for permission_classes [OK]
Common Mistakes:
Using instance instead of class in permission_classes
Forgetting to wrap in list or tuple
Using wrong attribute name
3. Given this DRF view snippet, what will happen if an anonymous user tries to access it?
from rest_framework.permissions import IsAuthenticated
from rest_framework.views import APIView
class MyView(APIView):
permission_classes = [IsAuthenticated]
def get(self, request):
return Response({'message': 'Hello'})
medium
A. The user will receive a 401 Unauthorized response
This permission denies access to anonymous users and returns 401 Unauthorized.
Step 2: Analyze the code behavior for anonymous user
Since the user is not logged in, DRF returns 401, not 403 or success.
Final Answer:
The user will receive a 401 Unauthorized response -> Option A
Quick Check:
IsAuthenticated denies anonymous with 401 [OK]
Hint: IsAuthenticated returns 401 for anonymous users [OK]
Common Mistakes:
Confusing 401 Unauthorized with 403 Forbidden
Expecting anonymous users to see data
Thinking code has syntax errors
4. Identify the error in this custom permission class:
from rest_framework.permissions import BasePermission
class IsOwner(BasePermission):
def has_object_permission(self, request, view, obj):
return obj.owner == request.user
# Usage in view
class MyView(APIView):
permission_classes = [IsOwner()]
def get(self, request, pk):
obj = get_object(pk)
self.check_object_permissions(request, obj)
return Response({'id': obj.id})
medium
A. get_object method is undefined
B. has_object_permission method is missing a return statement
C. check_object_permissions is called incorrectly
D. Permission class should be passed as class, not instance
Solution
Step 1: Check how permission_classes should be set
DRF expects permission classes, not instances, so use IsOwner without parentheses.
Step 2: Review other parts for errors
has_object_permission returns correctly, check_object_permissions usage is correct, get_object assumed defined elsewhere.
Final Answer:
Permission class should be passed as class, not instance -> Option D
Quick Check:
Use class names, not instances in permission_classes [OK]
Hint: Use class names, not instances, in permission_classes [OK]
Common Mistakes:
Passing permission instances instead of classes
Assuming missing return in has_object_permission
Confusing method calls with errors
5. You want to create a custom permission that allows access only if the user is authenticated and is the owner of the object. Which is the correct way to combine built-in and custom permissions in DRF?
hard
A. Set permission_classes = [IsAuthenticatedOrReadOnly, IsOwner] and override has_permission in IsOwner
B. Set permission_classes = [IsOwner] only and check authentication inside IsOwner
C. Set permission_classes = [IsAuthenticated, IsOwner] and implement has_object_permission in IsOwner
D. Set permission_classes = [IsAuthenticated()] and call IsOwner manually in the view
Solution
Step 1: Understand combining permissions in DRF
DRF checks all permissions in the list; all must allow access.
Step 2: Check how to combine authentication and ownership
Use IsAuthenticated to check login, and IsOwner to check object ownership via has_object_permission.
Step 3: Evaluate options
Set permission_classes = [IsAuthenticated, IsOwner] and implement has_object_permission in IsOwner correctly combines both permissions. Set permission_classes = [IsOwner] only and check authentication inside IsOwner misses separate authentication check. Set permission_classes = [IsAuthenticatedOrReadOnly, IsOwner] and override has_permission in IsOwner mixes permission types incorrectly. Set permission_classes = [IsAuthenticated()] and call IsOwner manually in the view uses instance and manual calls, which is not standard.
Final Answer:
Set permission_classes = [IsAuthenticated, IsOwner] and implement has_object_permission in IsOwner -> Option C
Quick Check:
Combine permissions in list for layered checks [OK]
Hint: List all needed permissions in permission_classes [OK]
Common Mistakes:
Skipping IsAuthenticated when ownership matters
Using instances instead of classes
Trying to call permissions manually
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.