Naresh
Naresh

Reputation: 1962

Method Decorator in class based view is not working

I am using class based view and applied permission classes only on POST method using method decorator. Till yesterday it was working but all of sudden it stopped. I am unable to find issue.

class OfferCreateListView(ListCreateAPIView):
    serializer_class = OfferSerializer
    queryset = Offers.objects.filter(user__isnull=True)

    @method_decorator(permission_classes((IsAuthenticated,)))
    @method_decorator(authentication_classes((BasicAuthentication, SessionAuthentication, TokenAuthentication,)))
    def post(self, request, *args, **kwargs):
        return super(OfferCreateListView, self).post(request, *args, **kwargs)

Where i am doing wrong. Is there any setting for this to work??

Upvotes: 0

Views: 1244

Answers (1)

Alasdair
Alasdair

Reputation: 308789

The permission_classes and authentication_classes decorators are designed for function based views. I haven't followed the rest framework code all the way through, but I'm surprised that it worked until yesterday -- I don't the decorators are intended to be used with class based views.

Instead, set attributes on the class. Since you only want the permission class to be applied for post requests, it sounds like you want IsAuthenticatedOrReadOnly.

class OfferCreateListView(ListCreateAPIView):
    permission_classes = (IsAuthenticatedOrReadOnly,)
    authentication_classes = (BasicAuthentication, SessionAuthentication, TokenAuthentication,)
    serializer_class = OfferSerializer
    queryset = Offers.objects.filter(user__isnull=True)

Upvotes: 2

Related Questions