DRF Authentication & Permissions

DRF authentication identifies the API caller (via token, session, or JWT), and permission classes decide whether that caller may perform the request. You set them per view or globally in REST_FRAMEWORK settings.

Django

DRF separates authentication (who is calling) from permissions (what they may do), each as pluggable classes. Authentication classes include SessionAuthentication (for the browsable API), TokenAuthentication (a static token per user), and JWT via the simplejwt package; they set request.user.

Permission classes then gate access: IsAuthenticated requires a logged-in caller, AllowAny opens the endpoint, IsAdminUser restricts to staff, and IsAuthenticatedOrReadOnly allows anonymous reads but authenticated writes. You can write custom permissions for object-level rules like 'only the owner may edit'.

Set defaults in the REST_FRAMEWORK dict (DEFAULT_AUTHENTICATION_CLASSES, DEFAULT_PERMISSION_CLASSES) and override per view with authentication_classes and permission_classes. Token-based schemes suit APIs because they are stateless, unlike cookie sessions.

Syntax

permission_classes = [IsAuthenticated]

Examples

Python

from rest_framework.permissions import IsAuthenticatedOrReadOnly
from rest_framework import viewsets

class PostViewSet(viewsets.ModelViewSet):
    queryset = Post.objects.all()
    serializer_class = PostSerializer
    permission_classes = [IsAuthenticatedOrReadOnly]

Learn it in a lesson

See also

Back to the Dictionary