1

I have ViewSet like this

class CustomViewSet(
    RouteActionArgumentsMixin,
    viewsets.ModelViewSet
):
    serializer_class = MySerializer

    def get_queryset(self):
        MyModel.objects.filter(...)

I want to add an action in this particular ViewSet which is based on the other queryset (let's say it is based on all MyModel objects).

@action(
    detail=True,
    methods=['post'],
    serializer_class=OtherSerializer
)
def make_action(self, *args, **kwargs):
    instance = self.get_object()
    serializer = self.get_serializer(instance, data=self.request.data, partial=True)
    serializer.is_valid(raise_exception=True)
    serializer.save()
    return Response(serializer.data)

How can I retrieve self.get_object() based on all MyModel objects? Can I change queryset for this particular action?

alis01
  • 169
  • 1
  • 3
  • 11
  • 3
    Just don't call `self.get_object()` and get the object in the method? – Iain Shelvington Feb 25 '21 at 12:36
  • 1
    Thank you. How can I do that? Get pk from URL and take instance by `objects.get()`? There is also a problem with some permissions, which are checked in `get_queryset()` – alis01 Feb 25 '21 at 12:44

0 Answers0