From 314daaedfac3fb49d0b99648bb92bcd786c7805e Mon Sep 17 00:00:00 2001 From: borzdyko Date: Fri, 21 Aug 2015 18:13:13 +0300 Subject: [PATCH 1/3] Using https://docs.djangoproject.com/en/1.8/ref/models/querysets/#regex for search filter by starting field with '$' --- rest_framework/filters.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rest_framework/filters.py b/rest_framework/filters.py index e05d31ae36..931263670f 100644 --- a/rest_framework/filters.py +++ b/rest_framework/filters.py @@ -94,6 +94,8 @@ def construct_search(self, field_name): 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 From 21d0e5183139e421f7f2b9fd96d765e9a1563b65 Mon Sep 17 00:00:00 2001 From: borzdyko Date: Fri, 21 Aug 2015 18:23:39 +0300 Subject: [PATCH 2/3] Test for regex search filter --- tests/test_filters.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/test_filters.py b/tests/test_filters.py index 9db685c286..0610b08557 100644 --- a/tests/test_filters.py +++ b/tests/test_filters.py @@ -407,6 +407,23 @@ class SearchListView(generics.ListAPIView): ] ) + 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) From f0782b9451be3b04dd4239d1199ea6a7278edd21 Mon Sep 17 00:00:00 2001 From: borzdyko Date: Fri, 21 Aug 2015 18:26:23 +0300 Subject: [PATCH 3/3] Doc for regex search filter --- docs/api-guide/filtering.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/api-guide/filtering.md b/docs/api-guide/filtering.md index e65c6bf4f3..6f4648cfd0 100644 --- a/docs/api-guide/filtering.md +++ b/docs/api-guide/filtering.md @@ -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: pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy