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

Add search_fields attribute to SearchFilter #8834

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 16 additions & 1 deletion docs/api-guide/filtering.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `SearchFilter` class itself or 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`.

from rest_framework import filters

Expand Down Expand Up @@ -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].

---
Expand Down
7 changes: 4 additions & 3 deletions rest_framework/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,15 @@ class SearchFilter(BaseFilterBackend):
}
search_title = _('Search')
search_description = _('A search term.')
search_fields = None
ad-m-ss marked this conversation as resolved.
Show resolved Hide resolved

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 search backend / view, 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', None)
return getattr(self, 'search_fields') or getattr(view, 'search_fields', None)

def get_search_terms(self, request):
"""
Expand Down
37 changes: 37 additions & 0 deletions tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,43 @@ class SearchListView(generics.ListAPIView):
{'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')
Expand Down