Performance: DRF permissions
This affects the server response time and user experience by controlling access checks before processing requests.
Jump into concepts and practice - no test required
from rest_framework.permissions import IsAuthenticated, DjangoModelPermissions from rest_framework.views import APIView from rest_framework.response import Response class MyView(APIView): permission_classes = [IsAuthenticated, DjangoModelPermissions] def get(self, request): # process request return Response(data)
class MyView(APIView): def get(self, request): if not request.user.is_authenticated: return Response(status=403) if not request.user.has_perm('app.view_model'): return Response(status=403) # process request return Response(data)
| Pattern | Server CPU Usage | Code Duplication | Response Delay | Verdict |
|---|---|---|---|---|
| Manual permission checks in each view | High (repeated checks) | High | Increased delay | [X] Bad |
| DRF built-in permission classes | Low (centralized checks) | Low | Minimal delay | [OK] Good |
IsAuthenticated to a DRF view?permission_classes.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'})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})