Bird
0
0

Identify the error in this custom permission class:

medium📝 Debug Q14 of 15
Django - DRF Advanced Features
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})
Aget_object method is undefined
Bhas_object_permission method is missing a return statement
Ccheck_object_permissions is called incorrectly
DPermission class should be passed as class, not instance
Step-by-Step Solution
Solution:
  1. Step 1: Check how permission_classes should be set

    DRF expects permission classes, not instances, so use IsOwner without parentheses.
  2. Step 2: Review other parts for errors

    has_object_permission returns correctly, check_object_permissions usage is correct, get_object assumed defined elsewhere.
  3. Final Answer:

    Permission class should be passed as class, not instance -> Option D
  4. Quick Check:

    Use class names, not instances in permission_classes [OK]
Quick Trick: Use class names, not instances, in permission_classes [OK]
Common Mistakes:
MISTAKES
  • Passing permission instances instead of classes
  • Assuming missing return in has_object_permission
  • Confusing method calls with errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes