Skip to content

Commit eb361d2

Browse files
authored
SearchFilter.get_search_terms returns list. (#9338)
1 parent 400b4c5 commit eb361d2

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

Diff for: rest_framework/filters.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,19 @@ def search_smart_split(search_terms):
2424
"""generator that first splits string by spaces, leaving quoted phrases together,
2525
then it splits non-quoted phrases by commas.
2626
"""
27+
split_terms = []
2728
for term in smart_split(search_terms):
2829
# trim commas to avoid bad matching for quoted phrases
2930
term = term.strip(',')
3031
if term.startswith(('"', "'")) and term[0] == term[-1]:
3132
# quoted phrases are kept together without any other split
32-
yield unescape_string_literal(term)
33+
split_terms.append(unescape_string_literal(term))
3334
else:
3435
# non-quoted tokens are split by comma, keeping only non-empty ones
35-
yield from (sub_term.strip() for sub_term in term.split(',') if sub_term)
36+
for sub_term in term.split(','):
37+
if sub_term:
38+
split_terms.append(sub_term.strip())
39+
return split_terms
3640

3741

3842
class BaseFilterBackend:
@@ -85,7 +89,8 @@ def get_search_terms(self, request):
8589
"""
8690
value = request.query_params.get(self.search_param, '')
8791
field = CharField(trim_whitespace=False, allow_blank=True)
88-
return field.run_validation(value)
92+
cleaned_value = field.run_validation(value)
93+
return search_smart_split(cleaned_value)
8994

9095
def construct_search(self, field_name, queryset):
9196
lookup = self.lookup_prefixes.get(field_name[0])
@@ -163,7 +168,7 @@ def filter_queryset(self, request, queryset, view):
163168
reduce(
164169
operator.or_,
165170
(models.Q(**{orm_lookup: term}) for orm_lookup in orm_lookups)
166-
) for term in search_smart_split(search_terms)
171+
) for term in search_terms
167172
)
168173
queryset = queryset.filter(reduce(operator.and_, conditions))
169174

0 commit comments

Comments
 (0)