File tree 3 files changed +20
-0
lines changed
3 files changed +20
-0
lines changed Original file line number Diff line number Diff line change @@ -260,6 +260,7 @@ The search behavior may be restricted by prepending various characters to the `s
260
260
* '^' Starts-with search.
261
261
* '=' Exact matches.
262
262
* '@' Full-text search. (Currently only supported Django's MySQL backend.)
263
+ * '$' Regex search.
263
264
264
265
For example:
265
266
Original file line number Diff line number Diff line change @@ -94,6 +94,8 @@ def construct_search(self, field_name):
94
94
return "%s__iexact" % field_name [1 :]
95
95
elif field_name .startswith ('@' ):
96
96
return "%s__search" % field_name [1 :]
97
+ if field_name .startswith ('$' ):
98
+ return "%s__iregex" % field_name [1 :]
97
99
else :
98
100
return "%s__icontains" % field_name
99
101
Original file line number Diff line number Diff line change @@ -407,6 +407,23 @@ class SearchListView(generics.ListAPIView):
407
407
]
408
408
)
409
409
410
+ def test_regexp_search (self ):
411
+ class SearchListView (generics .ListAPIView ):
412
+ queryset = SearchFilterModel .objects .all ()
413
+ serializer_class = SearchFilterSerializer
414
+ filter_backends = (filters .SearchFilter ,)
415
+ search_fields = ('$title' , '$text' )
416
+
417
+ view = SearchListView .as_view ()
418
+ request = factory .get ('/' , {'search' : 'z{2} ^b' })
419
+ response = view (request )
420
+ self .assertEqual (
421
+ response .data ,
422
+ [
423
+ {'id' : 2 , 'title' : 'zz' , 'text' : 'bcd' }
424
+ ]
425
+ )
426
+
410
427
def test_search_with_nonstandard_search_param (self ):
411
428
with override_settings (REST_FRAMEWORK = {'SEARCH_PARAM' : 'query' }):
412
429
reload_module (filters )
You can’t perform that action at this time.
0 commit comments