Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
django-rest-framework
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
django-rest-framework
Commits
8527a2c9
Commit
8527a2c9
authored
Aug 21, 2015
by
Tom Christie
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #3316 from purple-sunrise/iregexp_search
Regex search option
parents
f601c6c1
f0782b94
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
20 additions
and
0 deletions
+20
-0
docs/api-guide/filtering.md
+1
-0
rest_framework/filters.py
+2
-0
tests/test_filters.py
+17
-0
No files found.
docs/api-guide/filtering.md
View file @
8527a2c9
...
...
@@ -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:
...
...
rest_framework/filters.py
View file @
8527a2c9
...
...
@@ -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
...
...
tests/test_filters.py
View file @
8527a2c9
...
...
@@ -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
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment