Skip to content

Return empty array when query param doesn't match record instead of "Select a valid choice. That choice is not one of the available choices." #1529

Answered by carltongibson
cheslijones asked this question in Q&A
Discussion options

You must be logged in to vote

You can do something like this:

class StrictDjangoFilterBackend(django_filters.rest_framework.DjangoFilterBackend):
    """Return no results if the query doesn't validate."""

    def filter_queryset(self, request, queryset, view):
        try:
            return super().filter_queryset(request, queryset, view):
        except serializers.ValidationError:
            return queryset.none()

See #788 (comment) and surrounding comments.

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@cheslijones
Comment options

Answer selected by cheslijones
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants