5π
β
To achieve this you will need a custom filter backend extending the rest_framework.filters.SearchFilter
class. Specifically in the rest_framework.filters.SearchFilter
class there is a method get_search_terms
:
def get_search_terms(self, request):
"""
Search terms are set by a ?search=... query parameter,
and may be comma and/or whitespace delimited.
"""
params = request.query_params.get(self.search_param, '')
return params.replace(',', ' ').split()
We can override this method in our own CustomSearchFilter
class to take control of how you specify the search terms within the url, for example:
class CustomSearchFilter(SearchFilter):
search_field_prefix = "search_"
def get_search_terms(self, request):
# get search fields from the class
search_fields = getattr(request.resolver_match.func.view_class, 'search_fields', list())
params = []
# iterate over each query paramater in the url
for query_param in request.query_params:
# check if query paramater is a search paramater
if query_param.startswith(CustomSearchFilter.search_field_prefix):
# extrapolate the field name while handling cases where <CustomSearchFilter.search_field_prefix> is in the field name
field = CustomSearchFilter.search_field_prefix.join(
query_param.split(CustomSearchFilter.search_field_prefix)[1:]
)
# only apply search filter for fields that are in the views search_fields
if field in search_fields:
params.append(request.query_params.get(query_param, ''))
return params
Now replace the filters.SearchFilter
with your new CustomSearchFilter
in your views filter_backends
.
Hope this helps, Iβve written this without testing the code myself so let me know how you go!
- [Django]-Css not working with FastCGI and Lighttpd
- [Django]-How to add if statement in filter?
- [Django]-Filtering queryset if one value is greater than another value
- [Django]-How to restrict static files to the user who uploaded them in Django?
- [Django]-Django-Debug-Toolbar is not showing
Source:stackexchange.com