[Fixed]-How to return data with 403 error in Django Rest Framework?

37👍

from rest_framework import permissions
from rest_framework.exceptions import PermissionDenied

class CustomPerm(permissions.BasePermission):
    def has_object_permission(self, request, view, obj):
        if request.user.is_staff:
            return True
        raise PermissionDenied({"message":"You don't have permission to access",
                                "object_id": obj.id})

and you no need to override retrieve method

class CustomViewSet(...):
    model = Model
    permission_classes = (CustomPerm, )

 

Leave a comment