【问题标题】:django's filter SearchFilter isn't filtering the results, it returns me all of the objectsdjango 的过滤器 SearchFilter 没有过滤结果,它返回我所有的对象
【发布时间】:2020-02-12 20:27:16
【问题描述】:

我正在使用 Django 并尝试通过 SearchFilter 过滤我的响应数据

http://localhost:8000/autocomplete/a/?search=Something

它返回所有数据对象的问题。就像根本没有过滤器一样

http://localhost:8000/autocomplete/a/

我的意见.py:


from autocomplete.models import Autocomplete
from autocomplete.serializers import AutcompleteSerializer
from rest_framework import generics
from rest_framework.views import APIView
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework.filters import OrderingFilter, SearchFilter


class AutocompleteListView(generics.ListAPIView):
    serializer_class = AutcompleteSerializer
    queryset = Autocomplete.objects.all()
    filter_backends = (DjangoFilterBackend, SearchFilter, OrderingFilter)
    filter_fields = ("IATA", "IATAcity") # < this is working
    ordering_fields = ("AirportName") # < not working
    search_fields = ("IATA", "IATAcity") # < not working

在最后一条评论中看到了可能的解决方案:

Django REST - SearchFilter not filtering

但我真的不知道我应该在哪里发布它。

我做错了什么?

谢谢!!

【问题讨论】:

    标签: python django api filter backend


    【解决方案1】:

    您可以将您的search_fields 格式文件更改为列表

    【讨论】:

      【解决方案2】:

      找到了解决办法:

      确保settings.py 上没有'SEARCH_PARAM': 'SOMETHING'!!

      【讨论】:

        猜你喜欢
        • 2017-12-05
        • 1970-01-01
        • 2016-04-08
        • 2015-05-21
        • 1970-01-01
        • 2017-07-02
        • 1970-01-01
        • 2014-08-13
        • 2016-12-21
        相关资源
        最近更新 更多