From 854e22b6bca7b3866f1f3cade8b9676342a9553b Mon Sep 17 00:00:00 2001 From: Adam Dobrawy Date: Tue, 7 Feb 2023 11:48:41 +0100 Subject: [PATCH] Apply review commnents --- docs/api-guide/filtering.md | 17 ++++++++++++++++- rest_framework/filters.py | 4 ++-- rest_framework/mixins.py | 1 - tests/test_filters.py | 38 +++++++++++++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 4 deletions(-) diff --git a/docs/api-guide/filtering.md b/docs/api-guide/filtering.md index be8d10e9c..afb44ea3f 100644 --- a/docs/api-guide/filtering.md +++ b/docs/api-guide/filtering.md @@ -196,7 +196,7 @@ When in use, the browsable API will include a `SearchFilter` control: ![Search Filter](../img/search-filter.png) -The `SearchFilter` class will only be applied if the view has a `search_fields` attribute set. The `search_fields` attribute should be a list of names of text type fields on the model, such as `CharField` or `TextField`. +The `SearchFilter` class will only be applied if the view or `SearchFilter` class itself has a `search_fields` attribute set. The `search_fields` attribute should be a list of names of text type fields on the model, such as `CharField` or `TextField`. from rest_framework import filters @@ -243,6 +243,21 @@ To dynamically change search fields based on request content, it's possible to s return ['title'] return super().get_search_fields(view, request) +To use use multiple filters in the same view, override `search_param` and `search_fields` attribute. Many filters can be applied simultaneously on the same view. For example, the following subclass will search on `title` if the query parameter `search_title` is used and search on `text` if the query parameter `search_title` is used: + + from rest_framework import filters + + class TitleSearchFilter(filters.SearchFilter): + search_param = 'search_title' + search_fields = ('$title', ) + + class TextSearchFilter(filters.SearchFilter): + search_param = 'search_text' + search_fields = ('$text', ) + + class SearchListView(generics.ListAPIView): + filter_backends = (TitleSearchFilter, TextSearchFilter) + For more details, see the [Django documentation][search-django-admin]. --- diff --git a/rest_framework/filters.py b/rest_framework/filters.py index aaa476005..a0f480190 100644 --- a/rest_framework/filters.py +++ b/rest_framework/filters.py @@ -52,8 +52,8 @@ class SearchFilter(BaseFilterBackend): def get_search_fields(self, view, request): """ - Search fields are obtained from the view, but the request is always - passed to this method. Sub-classes can override this method to + Search fields are obtained from the view / search backend, but the request is + always passed to this method. Sub-classes can override this method to dynamically change the search fields based on request content. """ return getattr(view, 'search_fields', getattr(self, 'search_fields')) diff --git a/rest_framework/mixins.py b/rest_framework/mixins.py index 7fa8947cb..3828aed07 100644 --- a/rest_framework/mixins.py +++ b/rest_framework/mixins.py @@ -36,7 +36,6 @@ class ListModelMixin: """ def list(self, request, *args, **kwargs): queryset = self.filter_queryset(self.get_queryset()) - page = self.paginate_queryset(queryset) if page is not None: serializer = self.get_serializer(page, many=True) diff --git a/tests/test_filters.py b/tests/test_filters.py index 37ae4c7cf..910ec58a0 100644 --- a/tests/test_filters.py +++ b/tests/test_filters.py @@ -181,6 +181,44 @@ class SearchFilterTests(TestCase): {'id': 3, 'title': 'zzz', 'text': 'cde'} ] + def test_search_with_filter_multiple(self): + class TitleSearchFilter(filters.SearchFilter): + search_param = 'search_title' + search_fields = ('$title', ) + + class TextSearchFilter(filters.SearchFilter): + search_param = 'search_text' + search_fields = ('$text', ) + + class SearchListView(generics.ListAPIView): + queryset = SearchFilterModel.objects.all() + serializer_class = SearchFilterSerializer + filter_backends = (TitleSearchFilter, TextSearchFilter) + + view = SearchListView.as_view() + request = factory.get('/', {TitleSearchFilter.search_param: r'^z{3}$'}) + response = view(request) + assert response.data == [ + {'id': 3, 'title': 'zzz', 'text': 'cde'} + ] + + request = factory.get('/', {TextSearchFilter.search_param: r'^cde$'}) + response = view(request) + assert response.data == [ + {'id': 3, 'title': 'zzz', 'text': 'cde'} + ] + + request = factory.get('/', { + TitleSearchFilter.search_param: r'^(z{3}|z{2})$', + TextSearchFilter.search_param: r'^\w{3}$' + }) + response = view(request) + assert response.data == [ + {'id': 2, 'title': 'zz', 'text': 'bcd'}, + {'id': 3, 'title': 'zzz', 'text': 'cde'} + ] + + def test_search_field_with_null_characters(self): view = generics.GenericAPIView() request = factory.get('/?search=\0as%00d\x00f')