Django-filter: DRF integration does not work for extra viewset actions

Created on 16 Aug 2018  ·  9Comments  ·  Source: carltongibson/django-filter

I have multiple list actions in a single viewset by making use of the @action decorator as described here and have included a FilterSet via filter_class as specified in docs here. The filters are only being applied to the default ViewSet List and Retrieve actions and not any of the custom List or Retrieve actions which is where we actually need the filters.

Most helpful comment

I got it working by simply moving the call as follows

@action(methods=['GET'], detail=False)
def simplelist(self, request, *args, **kwargs):
    qs = self.filter_queryset(self.get_queryset())
    serializer = SpecializedModelListSerializer(qs, many=True)
    return Response(serializer.data, status=status.HTTP_200_OK)

Thanks for the help!!!

All 9 comments

Hi @estianross. You need to provide more details if we're going to be able to comment.

Are you sure your extra actions are calling filter_queryset()?

They do but I'm fairly sure I'm not using it the way it should be used if that's where the issue is, I've included a simple sample action below to show the basics of what I'm doing in these actions

@action(methods=['GET'], detail=False)
def simplelist(self, request, *args, **kwargs):
    serializer = SpecializedModelListSerializer(self.filter_queryset(self.get_queryset()), many=True)
        return Response(serializer.data, status=status.HTTP_200_OK)

I suggest you create an APITestCase and plug the url + query parameters you're expecting into it. You'll then be able to make assertions and insert breakpoints as needed to see where things are going wrong for you.

(I'm afraid that without much more detail it's impossible to be able to advise at all.)

I got it working by simply moving the call as follows

@action(methods=['GET'], detail=False)
def simplelist(self, request, *args, **kwargs):
    qs = self.filter_queryset(self.get_queryset())
    serializer = SpecializedModelListSerializer(qs, many=True)
    return Response(serializer.data, status=status.HTTP_200_OK)

Thanks for the help!!!

This does not seem to work in most of my cases.

I think this issue should be reopened. I have the same problem and the proposed solution worked for me.

@action(methods=['get'], detail=True, url_path='retrieve_by_card_no/(< int:CardNo> )')
def getByCarNo(self, request, CardNo):
user = get_object_or_404(employe_model, EmpCardNo=CardNo)
return Response(serializers.EmployeeInfoSerializer(user).data, status=status.HTTP_200_OK)

I want to search employee by his card no instead of employee id

anyone can help me?

I think the filter integration works correctly if you pass the query params along with your action in the URL. Below was the action that I had

@action(methods=['GET'], detail=False)
def export(self, request): 
    queryset = self.get_queryset()
    filtered_queryset = self.filter_queryset(queryset)

When trying to call the export action from DRF browsable API, the request that was getting sent was
/api/viewname/export/ instead it should called like
/api/viewname/export/?query_param_1=value1&?query_param_2=value2

Passing the query params along with the action will call the filterset class and hence you will get a filtered queryset in filtered_queryset variable

Thank you so much I figured it out

On Wed, Apr 28, 2021 at 12:32 AM Mohammed Sunasra @.*>
wrote:

I think the filter integration works correctly if you pass the query
params along with your action in the URL. Below was the action that I had

@action(methods=['GET'], detail=False)
def export(self, request):
queryset = self.get_queryset()
filtered_queryset = self.filter_queryset(queryset)

When trying to call the export action from DRF browsable API, the request
that was getting sent was
/api/viewname/export/ instead it should called like
/api/viewname/export/?query_param_1=value1&?query_param_2=value2

Passing the query params along with the action will call the filterset
class and hence you will get a filtered queryset in filtered_queryset
variable


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/carltongibson/django-filter/issues/967#issuecomment-828220562,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AMD2QQXC6FGISRWTHRAGUSTTK62Y7ANCNFSM4FP6FMMQ
.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jwineinger picture jwineinger  ·  3Comments

lalzada picture lalzada  ·  3Comments

hakib picture hakib  ·  3Comments

edmorley picture edmorley  ·  3Comments

csarcom picture csarcom  ·  3Comments