Custom permissions let you control who can do what in your Django app. They help keep your app safe and organized.
Custom permissions in Django
Start learning this pattern below
Jump into concepts and practice - no test required
from rest_framework.permissions import BasePermission class MyCustomPermission(BasePermission): def has_permission(self, request, view): # return True if user has permission return condition def has_object_permission(self, request, view, obj): # return True if user has permission for this object return condition
Custom permissions are classes that inherit from BasePermission.
Use has_permission for general checks and has_object_permission for object-specific checks.
from rest_framework.permissions import BasePermission class IsAdminUser(BasePermission): def has_permission(self, request, view): return request.user and request.user.is_staff
from rest_framework.permissions import BasePermission class IsOwner(BasePermission): def has_object_permission(self, request, view, obj): return obj.owner == request.user
This example shows a custom permission that allows access only if the user owns the item. The view checks this permission before responding.
from rest_framework.permissions import BasePermission from rest_framework.views import APIView from rest_framework.response import Response class IsOwnerPermission(BasePermission): def has_object_permission(self, request, view, obj): return obj.owner == request.user class Item: def __init__(self, owner): self.owner = owner class ItemView(APIView): permission_classes = [IsOwnerPermission] def get(self, request): item = Item(owner=request.user) self.check_object_permissions(request, item) return Response({'detail': 'You are the owner!'})
Always test your custom permissions to make sure they work as expected.
Use has_permission for general access and has_object_permission for checking specific objects.
Combine multiple permissions by listing them in permission_classes.
Custom permissions control user access in Django apps.
Create them by subclassing BasePermission and defining permission methods.
Use them in views to protect data and actions based on your rules.
Practice
Solution
Step 1: Understand what permissions do in Django
Permissions control what users can or cannot do in the app.Step 2: Identify the role of custom permissions
Custom permissions let you define your own rules for user access beyond default ones.Final Answer:
To control user access based on specific rules you define -> Option AQuick Check:
Custom permissions = control user access [OK]
- Thinking permissions change database structure
- Confusing permissions with performance settings
- Believing permissions create new tables
Solution
Step 1: Recall the BasePermission class methods
The main method to check access ishas_permission.Step 2: Confirm which method controls permission checks
has_permissionreturns True or False to allow or deny access.Final Answer:
has_permission -> Option CQuick Check:
Permission check method = has_permission [OK]
- Using get_queryset which filters data, not permissions
- Confusing save method with permission checks
- Using dispatch which is for request handling
from rest_framework.permissions import BasePermission
class IsAuthenticatedCustom(BasePermission):
def has_permission(self, request, view):
return request.user and request.user.is_authenticated
Solution
Step 1: Analyze the has_permission method logic
It returns True only if request.user exists and is authenticated.Step 2: Consider the case when user is not authenticated
Then request.user.is_authenticated is False, so method returns False denying access.Final Answer:
Access is denied because user is not authenticated -> Option BQuick Check:
Unauthenticated user = access denied [OK]
- Assuming access is granted without authentication
- Thinking code raises error due to return statement
- Confusing staff status with authentication
from rest_framework.permissions import BasePermission
class IsOwnerPermission(BasePermission):
def has_permission(self, request, view):
return request.user == view.get_object().owner
Solution
Step 1: Understand permission methods roles
has_permissionchecks general access;has_object_permissionchecks per object.Step 2: Identify misuse of has_permission for object ownership
Comparing user to object owner requireshas_object_permission, nothas_permission.Final Answer:
Using has_permission instead of has_object_permission for object check -> Option DQuick Check:
Object checks need has_object_permission [OK]
- Confusing has_permission with has_object_permission
- Assuming import errors cause this issue
- Thinking comparison operator is wrong
Solution
Step 1: Understand the requirement
User must be authenticated AND method must be safe (GET, HEAD, OPTIONS).Step 2: Analyze each option's logic
class SafeAndAuthenticated(BasePermission): def has_permission(self, request, view): return request.user.is_authenticated and request.method in ['GET', 'HEAD', 'OPTIONS'] uses AND with correct method list; others use OR, NOT, or wrong method checks.Final Answer:
class SafeAndAuthenticated(BasePermission): def has_permission(self, request, view): return request.user.is_authenticated and request.method in ['GET', 'HEAD', 'OPTIONS'] -> Option AQuick Check:
Use AND for combined conditions [OK]
- Using OR instead of AND allowing wrong access
- Checking for methods incorrectly with NOT
- Allowing unauthenticated users by mistake
