Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DRF integration does not work for extra viewset actions #967

Closed
estianross opened this issue Aug 16, 2018 · 9 comments
Closed

DRF integration does not work for extra viewset actions #967

estianross opened this issue Aug 16, 2018 · 9 comments

Comments

@estianross
Copy link

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.

@carltongibson
Copy link
Owner

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()?

@estianross
Copy link
Author

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)

@carltongibson
Copy link
Owner

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.)

@estianross
Copy link
Author

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!!!

@LyleScott
Copy link

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

@felipelerena
Copy link

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

@nabeeltariqbhatti
Copy link

nabeeltariqbhatti commented Apr 27, 2021

@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?

@Mohammed-Sunasra
Copy link

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

@nabeeltariqbhatti
Copy link

nabeeltariqbhatti commented Apr 28, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants