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

ViewSet.filter_queryset() not being honored, extra parameters #147

Closed
jtsay362 opened this issue Sep 8, 2020 · 4 comments
Closed

ViewSet.filter_queryset() not being honored, extra parameters #147

jtsay362 opened this issue Sep 8, 2020 · 4 comments

Comments

@jtsay362
Copy link

jtsay362 commented Sep 8, 2020

Describe the bug
I have these filter backends enabled by default on my DRF settings:
django_filters.rest_framework.DjangoFilterBackend, rest_framework.filters.SearchFilter,
rest_framework.filters.OrderingFilter

But I don't want those applied to non-list views and their presence is adding multiple undesired parameters to the API schema, so I override ViewSet.filter_queryset() to return the argument queryset unchanged if the action is not "list". This seems to disable these filters for non-list views.

However, when I run drf-spectacular, these query parameters remain in the documentation. It does not seem like filter_queryset() is being called during introspection.

To Reproduce
It would be most helpful to provide a small snippet to see how the bug was provoked.

In settings.py:

'DEFAULT_FILTER_BACKENDS': ('django_filters.rest_framework.DjangoFilterBackend',
                            'rest_framework.filters.SearchFilter',
                            'rest_framework.filters.OrderingFilter',),

Add this method to your viewset:

def filter_queryset(self, qs):
    """
    Don't apply filterset classes on non-list views, it's unnecessary.
    Maybe one day it will remove the OpenAPI documentation for the
    filter parameters on these methods, but it doesn't seem to work yet.
    """

    logger.debug(f"filter_queryset, {self.action=}")

    if self.action == 'list':
        return super().filter_queryset(qs)
    else:
        return qs

Then run drf-spectacular to generate the schema. "ordering" and "search" are documented as query parameters on the non-list endpoints.

Expected behavior
"ordering" and "search" should not appear as query parameters on the non-list endpoints.

@tfranzel
Copy link
Owner

hi @jtsay362 . that was one of the last pieces of legacy code. filters were indeed applied everywhere. now it should only apply to list views and work as expected.

fyi and independent of the bug: i also added improved typing support for django_filters. you can use the patched backend drf_spectacular.contrib.django_filters.DjangoFilterBackend

let me know if that works for you. cheers!

@tfranzel
Copy link
Owner

as far as i am concerned, this issue is resolved. @jtsay362 please inform me if that is not the case and i'll reopen. thx

@jtsay362
Copy link
Author

jtsay362 commented Sep 23, 2020

@tfranzel I am sorry I did not have a chance to respond. I am fairly new to python, so I had some trouble installing your package from Github. Thank you very much for doing this work. I will for sure give you some feedback when this makes it into the next release.

Update: I used 0.9.13 and saw that the non-list endpoints no longer have filter parameters, so this fixed it. Thanks so much!
I'll be reporting a different issue with drf_spectacular.contrib.django_filters.DjangoFilterBackend though.

@tfranzel
Copy link
Owner

hey @jtsay362, no worries. ideally we should confirm fixed before releases 😄 i'm glad though it works for you.

for the next issue consider this. that gives you the current master state for testing.

pip uninstall drf-spectacular
pip install git+https://github.com/tfranzel/drf-spectacular 

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

2 participants