django的过滤器SearchFilter没有过滤结果,它返回了我所有的对象

时间:2019-10-16 06:15:33

标签: python django api filter backend

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

  

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

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

  

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

我的views.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

但是我确实没有确切地发布它。

我在做什么错了?

谢谢!

2 个答案:

答案 0 :(得分:0)

您可以将search_fields格式的插条更改为列表

答案 1 :(得分:0)

找到了解决方案:

确保在<source>: In function 'f': <source>:4:12: warning: declaration of 'y' shadows a global declaration [-Wshadow] 4 | void f(int y){ | ~~~~^ <source>:2:11: note: shadowed declaration is here 2 | int w, x, y, z = 10; | ^ <source>: In function 'main': <source>:11:9: warning: declaration of 'w' shadows a global declaration [-Wshadow] 11 | int w; | ^ <source>:2:5: note: shadowed declaration is here 2 | int w, x, y, z = 10; | ^ Compiler returned: 0 上没有settings.py