question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add FilterSet parameter to @extend_schema

See original GitHub issue

Hello

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:open
  • Created a year ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
tfranzelcommented, Sep 7, 2022

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:

    def get_filterset_class(self):
       if getattr(self, 'swagger_fake_view', False):
            return AccountFilter
       else:
            return None
0reactions
mdefechecommented, Sep 9, 2022

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 ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

set responses parm for extend_schema will make auto ...
The best solution is providing an option to include the filter parameters because filter_backends are not restricted to use in list view. rest_framework.schemas ......
Read more >
Workflow & schema customization - drf-spectacular
GenericViewSet): @extend_schema( parameters=[ QuerySerializer, # serializer fields are ... You can apply it also to the method of a SerializerMethodField .
Read more >
Add some parameters to FilterSet (django-filter) + some ...
The answer from @Sherpa has just two slight problems. First, you should replace fields with filters . Second, you can't use += operator, ......
Read more >
Use the $filter query parameter - Microsoft Graph
Learn how to use the $filter OData query parameter and its operators against different types of properties in Microsoft Graph.
Read more >
PostGraphile | Filtering
This adds a filter argument to connections that enables you to use advanced filters, including filtering on related records from other tables, using...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found