Add FilterSet parameter to @extend_schema
See original GitHub issueHello
I’m using django-filters and for some methods I’m calling the filter manually without being able to easily specify it in @extend_schema.
Describe the bug I have a ClientViewSet with a method listing accounts belonging to a given client. In that method, I user a different filter than the ViewSet filterset_class. I cannot override the filterset_class with AccountFilter as it will be applied to self.get_object() which is handling Clients and not Accounts. Hence I manually call the AccountFilter to filter the accounts linked to the given client.
@action(detail=True, methods=["get"], filterset_class=None)
def accounts(self, request, client_number):
client = self.get_object()
queryset = Account.objects.filter(client=client)
filter = AccountFilter(self.request.query_params, request=request, queryset=queryset)
serializer = AccountSerializer(filter.qs, many=True)
return Response(serializer.data)
To Reproduce The only way I found to document this is by manually specifying the parameters in @extend_schema?
Expected behavior It would be great to be able to specify a FilterSet in @extend_schema when no filterset_class / filterset_fields can be set in the decorator. It would only serve documentation purpose
Thank you for the great library !
Issue Analytics
- State:
- Created a year ago
- Comments:5 (2 by maintainers)
I see your point. This is kind of an exception that is not that easy to accommodate. However, I think there might be a workaround.
You have to figure out the exact details, but you could return a different class on schema runs that would allow running the detection of the changed class without interfering with the queryset on actual requests. Something to the effect of:
I understand though I believe this situation is more than an edge case.
Is there a way to generate OpenAPIParameters based on a filterset ? If not, would this functionality make sense as an helper method ?