mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-22 17:47:04 +03:00
Merge pull request #3316 from purple-sunrise/iregexp_search
Regex search option
This commit is contained in:
commit
8527a2c9d2
|
@ -260,6 +260,7 @@ The search behavior may be restricted by prepending various characters to the `s
|
|||
* '^' Starts-with search.
|
||||
* '=' Exact matches.
|
||||
* '@' Full-text search. (Currently only supported Django's MySQL backend.)
|
||||
* '$' Regex search.
|
||||
|
||||
For example:
|
||||
|
||||
|
|
|
@ -94,6 +94,8 @@ class SearchFilter(BaseFilterBackend):
|
|||
return "%s__iexact" % field_name[1:]
|
||||
elif field_name.startswith('@'):
|
||||
return "%s__search" % field_name[1:]
|
||||
if field_name.startswith('$'):
|
||||
return "%s__iregex" % field_name[1:]
|
||||
else:
|
||||
return "%s__icontains" % field_name
|
||||
|
||||
|
|
|
@ -407,6 +407,23 @@ class SearchFilterTests(TestCase):
|
|||
]
|
||||
)
|
||||
|
||||
def test_regexp_search(self):
|
||||
class SearchListView(generics.ListAPIView):
|
||||
queryset = SearchFilterModel.objects.all()
|
||||
serializer_class = SearchFilterSerializer
|
||||
filter_backends = (filters.SearchFilter,)
|
||||
search_fields = ('$title', '$text')
|
||||
|
||||
view = SearchListView.as_view()
|
||||
request = factory.get('/', {'search': 'z{2} ^b'})
|
||||
response = view(request)
|
||||
self.assertEqual(
|
||||
response.data,
|
||||
[
|
||||
{'id': 2, 'title': 'zz', 'text': 'bcd'}
|
||||
]
|
||||
)
|
||||
|
||||
def test_search_with_nonstandard_search_param(self):
|
||||
with override_settings(REST_FRAMEWORK={'SEARCH_PARAM': 'query'}):
|
||||
reload_module(filters)
|
||||
|
|
Loading…
Reference in New Issue
Block a user