Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set authentication and permission only on PUT requests in django REST in viewsets?

I have a viewset subclassing from modelviewset, I add next:

authication_classes = [SessionAuthentication,BasicAuthentication]
permission_classes = [IsAuthenticated]

Then, got following message when list, detail/retrieve and put requests.

"detail": "Authentication credentials were not provided."

What should i change to only give this message when I update the data ??

like image 368
Nitesh Rawat Avatar asked Sep 15 '25 04:09

Nitesh Rawat


1 Answers

Override get_permissions method on the ModelViewSet class.

Example:

class FooViewSet(ModelViewSet):
    authentication_classes = (SessionAuthentication, BasicAuthentication, )
    permission_classes = (IsAuthenticated, )

    def get_permissions(self):
        if self.request.method != 'PUT':
            return []
        return super().get_permissions()
like image 125
Sachin Avatar answered Sep 17 '25 01:09

Sachin