【问题标题】:django SearchFilter is not filtering the list of queryset,it returns all of itdjango SearchFilter 没有过滤查询集列表,它返回所有查询集
【发布时间】:2017-12-05 09:45:21
【问题描述】:

这是我的viewviews.py文件

class StudentList(generics.ListAPIView):
queryset = Student.objects.all()
serializer_class = StudentSerializer
#pagination_class = StudentPageNumberPagination
filter_backends = [SearchFilter]
search_fields=['name','mobile']

这是我的序列化程序类

class StudentSerializer(serializers.ModelSerializer):
class Meta:
    model=Student
    fields=('id','name','mobile','time','late','date')

这是我在浏览器中输入的内容

http://192.168.0.118:8000/students/?name=ket

我得到了数据库中的所有项目

【问题讨论】:

    标签: django python-3.x django-rest-framework filtering


    【解决方案1】:

    如果您使用 SearchFilter,您的搜索字符串必须在搜索参数中传递,如下所示: http://192.168.0.118:8000/students/?search=ket 您可以在docs 中了解它。如果要将查询参数拆分为特定字段,可以使用 DjangoFilterBackend 而不是 SearchFilter。

    【讨论】:

      猜你喜欢
      • 2018-12-01
      • 1970-01-01
      • 2020-06-22
      • 2018-09-22
      • 2019-08-08
      • 2020-08-22
      • 2019-04-15
      • 2011-09-29
      相关资源
      最近更新 更多