Django REST - @action decorator "Unresolved reference 'action'"

Viewed 916

I'm trying to use the @action decorator in my views.py but it's not being recognized. Is it deprecated or something?

enter image description here

class CategoryTargetGroupDelete(APIView):
    queryset = CategoryTargetGroup.objects.all()

    @action(detail=False, methods=['delete'])
    def delete(self, request):
        cat_id = request.query_params['cat_id']
        queryset = CategoryTargetGroup.objects.filter(category=cat_id)
        queryset.delete()
        return Response(serializer.data)

I have tried importing action..

enter image description here

DRF 3.7.1

Python 3.9

3 Answers

@action decorator added to rest_framework in version 3.8.0. You can increase rest_framework version or use @detail_route or @list_route instead of @action decorator

more detail here

Well without any additional infos like the drf version its kind of hard. but do you get the action from here?

from rest_framework.decorators import action

I think you might had not imported actions.

Try

from rest_framework.decorators import action
Related