Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita2bb1fb

Browse files
committed
Addunaccent toSearchFilter
1 parent9864c47 commita2bb1fb

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

‎docs/api-guide/filtering.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ The search behavior may be specified by prefixing field names in `search_fields`
230230
|`$`|`iregex`| Regex search.|
231231
|`@`|`search`| Full-text search (Currently only supported Django's[PostgreSQL backend][postgres-search]).|
232232
| None|`icontains`| Contains search (Default).|
233+
|`&`|`unaccent`| Accent-insensitive search. (Currently only supported Django's[PostgreSQL backend][postgres-lookups]).|
233234

234235
For example:
235236

@@ -370,3 +371,4 @@ The [djangorestframework-word-filter][django-rest-framework-word-search-filter]
370371
[HStoreField]:https://docs.djangoproject.com/en/3.0/ref/contrib/postgres/fields/#hstorefield
371372
[JSONField]:https://docs.djangoproject.com/en/3.0/ref/contrib/postgres/fields/#jsonfield
372373
[postgres-search]:https://docs.djangoproject.com/en/stable/ref/contrib/postgres/search/
374+
[postgres-lookups]:https://docs.djangoproject.com/en/stable/ref/contrib/postgres/lookups/#unaccent

‎rest_framework/filters.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class SearchFilter(BaseFilterBackend):
6868
'=':'iexact',
6969
'@':'search',
7070
'$':'iregex',
71+
'&':'unaccent',
7172
}
7273
search_title=_('Search')
7374
search_description=_('A search term.')

‎tests/test_filters.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
fromdjango.test.utilsimportoverride_settings
1111

1212
fromrest_frameworkimportfilters,generics,serializers
13-
fromrest_framework.compatimportcoreschema
13+
fromrest_framework.compatimportcoreschema,postgres_fields
1414
fromrest_framework.exceptionsimportValidationError
1515
fromrest_framework.testimportAPIRequestFactory
1616

@@ -305,6 +305,25 @@ class SearchListView(generics.ListAPIView):
305305
]
306306

307307

308+
@pytest.mark.skipif(notpostgres_fields,reason='psycopg2 is not installed')
309+
classSearchPostgreSQLFilterTests(TestCase):
310+
311+
deftest_unaccent_search(self):
312+
classSearchListView(generics.ListAPIView):
313+
queryset=SearchFilterModel.objects.all()
314+
serializer_class=SearchFilterSerializer
315+
filter_backends= (filters.SearchFilter,)
316+
search_fields= ('title','&text')
317+
318+
obj=SearchFilterModel.objects.create(title='Accent títle',text='Accent téxt')
319+
view=SearchListView.as_view()
320+
request=factory.get('/', {'search':'accent text'})
321+
response=view(request)
322+
assertresponse.data== [
323+
{'id':obj.id,'title':'Accent títle','text':'Accent téxt'}
324+
]
325+
326+
308327
classAttributeModel(models.Model):
309328
label=models.CharField(max_length=32)
310329

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp