Django-rest-framework将具有一个SearchFilter
后端,该后端将允许对可搜索字段进行单个查询:
class OrganizationViewSet(viewsets.ModelViewSet):
queryset = Organization.objects.all()
serializer_class = OrganizationSerializer
pagination_class = CustomPagination
filter_backends = (filters.SearchFilter, DjangoFilterBackend)
filter_fields = ('sector', 'industry', 'marketplace')
search_fields = ('symbol',)
这样,当我查询...?search=AMZ
时,它只会返回symbol
字段中具有非敏感匹配项的记录。
如果我在search_fields
元组中添加另一个元素,它将在两者中查找相同的搜索字符串。
有没有一种方法可以单独定义这些搜索字段,因此我可以执行以下操作:
?search_symbol=AMZ&search_name=Good
,以便查找在AMZ
字段中具有symbol
和在good
字段中具有name
的对象?
答案 0 :(得分:1)
要实现此目的,您将需要一个扩展rest_framework.filters.SearchFilter
类的自定义过滤器后端。特别是在rest_framework.filters.SearchFilter
类中,有一种方法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()
我们可以在我们自己的CustomSearchFilter
类中重写此方法,以控制如何在url中指定搜索词,例如:
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
现在在视图filters.SearchFilter
中用新的CustomSearchFilter
替换filter_backends
。
希望这会有所帮助,我编写此代码时并未亲自测试代码,所以请告诉我您的情况!
答案 1 :(得分:0)
您应该考虑改用django-filter
。可能会让您的生活更轻松。