Django REST-SearchFilter不过滤 [英] Django REST - SearchFilter not filtering

查看:158
本文介绍了Django REST-SearchFilter不过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Django 1.10 django-rest-framework 3.5.3
我想要一个通用的搜索查询:在许多字段上搜索一个值。
我在文档上找到了 SearchFilter

我试图将过滤器后端添加到ViewSet中,但似乎无法正常工作。任何包含所有对象的搜索查询响应。

I tried to add the filter backend to the ViewSet, but it seems not to be working. Any search query response with all the objects.

ViewSet:

from rest_framework import filters

class UserViewSet(viewsets.ModelViewSet):
    queryset = User.objects.all().order_by('-date_joined')
    serializer_class = UserSerializer
    filter_backends = (filters.SearchFilter,)
    filter_fields = ['username', 'email', 'is_staff', 'groups']

查询网址:

http://localhost:8000/users/?search=something

此外,在文档中,他们还显示了一个新按钮过滤器 django其余网页。

In addition, at the docs they show that a new button "Filter" added to the django rest web page. It does not in my case.

推荐答案

而不是 filter_fields 使用 search_fields filter_fields 用于启用对特定字段的过滤,例如 / users /?username = something& is_staff = True

Instead of filter_fields use search_fields. filter_fields is for enabling filtering on specific fields, like /users/?username=something&is_staff=True.

这篇关于Django REST-SearchFilter不过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆