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

Fix: SearchFilter renders search field with invalid value #9023

Merged
merged 1 commit into from
Jul 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions rest_framework/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,9 @@ def to_html(self, request, queryset, view):
if not getattr(view, 'search_fields', None):
return ''

term = self.get_search_terms(request)
term = term[0] if term else ''
context = {
'param': self.search_param,
'term': term
'term': request.query_params.get(self.search_param, ''),
}
template = loader.get_template(self.template)
return template.render(context)
Expand Down
17 changes: 17 additions & 0 deletions tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,23 @@ def as_sql(self, compiler, connection):
{'id': 2, 'title': 'zz', 'text': 'bcd'},
]

def test_search_field_with_multiple_words(self):
class SearchListView(generics.ListAPIView):
queryset = SearchFilterModel.objects.all()
serializer_class = SearchFilterSerializer
filter_backends = (filters.SearchFilter,)
search_fields = ('title', 'text')

search_query = 'foo bar,baz'
view = SearchListView()
request = factory.get('/', {'search': search_query})
request = view.initialize_request(request)

rendered_search_field = filters.SearchFilter().to_html(
request=request, queryset=view.queryset, view=view
)
assert search_query in rendered_search_field


class AttributeModel(models.Model):
label = models.CharField(max_length=32)
Expand Down