1👍
✅
Generic View (and hence all classes that inherit from it) in DRF has a filter_queryset
method which is called by the various mixins to perform filtering, so you can simply call that in your method. If you also want pagination there are the methods paginate_queryset
and get_paginated_response
:
class ApartmentViewset(viewsets.ModelViewSet):
...
@action(detail=False)
def sold(self, request):
queryset = self.filter_queryset(self.queryset.filter(issold=True))
serialize = self.serializer_class(queryset, many=True)
return Response(serialize.data)
Source:stackexchange.com