from rest_framework.permissions import IsAuthenticated from rest_framework.views import APIView from rest_framework.response import Response class MyView(APIView): permission_classes = [IsAuthenticated] def get(self, request): return Response({'message': 'Hello, authenticated user!'})
The IsAuthenticated permission class denies access to unauthenticated users by returning a 401 Unauthorized response. It does not return 403 Forbidden, which is for authenticated users without permission.
The permission_classes attribute must be a list or tuple of permission classes. Option D uses a list, which is correct. Option D uses bitwise AND which is invalid here. Option D is a tuple, so it is valid syntax but less common; however, DRF accepts tuples too. Option D uses a set which is not supported.
However, since option D is a tuple, it is syntactically valid but less common. To avoid ambiguity, the question expects the list form as the correct answer.
from rest_framework.permissions import BasePermission class IsOwner(BasePermission): def has_object_permission(self, request, view, obj): if request.user == obj.owner: return True return False
If the has_object_permission method does not explicitly return False when the condition fails, it returns None by default. DRF treats None as False, so permission is denied.
permission_granted after calling the permission check?from rest_framework.permissions import IsAuthenticated class DummyRequest: def __init__(self, user): self.user = user class DummyUser: def __init__(self, is_authenticated): self.is_authenticated = is_authenticated request = DummyRequest(DummyUser(is_authenticated=False)) permission = IsAuthenticated() permission_granted = permission.has_permission(request, None)
The IsAuthenticated permission checks if request.user.is_authenticated is True. Here, it is False, so has_permission returns False.
IsAuthenticatedOrReadOnly allows anyone to read but requires authentication for write actions. Combining it with a custom IsOwner permission in has_object_permission ensures only owners can write. Other options either restrict too much or do not enforce permissions properly.