Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用';搜索过滤器';我在尝试搜索时出错_Python_Django_Django Rest Framework - Fatal编程技术网

Python 使用';搜索过滤器';我在尝试搜索时出错

Python 使用';搜索过滤器';我在尝试搜索时出错,python,django,django-rest-framework,Python,Django,Django Rest Framework,当我在api中使用搜索函数时,我得到一个错误,上面写着“无法将关键字‘q/c’解析到字段中。选项是:‘XXX’”。q或c取决于我所在的api页面。“c”代表我的选择,而“q”代表问题 from rest_framework import generics from . serializers import QuestionSerializer, ChoiceSerializer from django_filters.rest_framework import DjangoFilterB

当我在api中使用搜索函数时,我得到一个错误,上面写着“无法将关键字‘q/c’解析到字段中。选项是:‘XXX’”。q或c取决于我所在的api页面。“c”代表我的选择,而“q”代表问题

from rest_framework import generics  
from . serializers import QuestionSerializer, ChoiceSerializer  
from django_filters.rest_framework import DjangoFilterBackend  
from rest_framework.filters import OrderingFilter, SearchFilter


from polls.models import Choice, Question


# need to create a view for each serializer class QuestionList(generics.ListAPIView):
    serializer_class = QuestionSerializer  # set the serializer
    queryset = Question.objects.all()  # set the query set to all the objects of that view
    filter_backends = (DjangoFilterBackend, OrderingFilter, SearchFilter)  # allows for filters
    filter_fields = {'id': ['gte', 'lte', 'exact'], 'pub_date': ['gte', 'lte']}
    ordering_fields = ('id', 'question_text', 'pub_date')
    search_fields = 'question_text'


class ChoiceList(generics.ListAPIView):
    serializer_class = ChoiceSerializer
    queryset = Choice.objects.all()
    filter_backends = (DjangoFilterBackend, OrderingFilter, SearchFilter)
    # trying to do greater than or less than filter fields stuff
    filter_fields = {'id': ['gte', 'lte', 'exact'], 'votes': ['gte', 'lte', 'exact']}
    ordering_fields = ('id', 'votes', 'choice_text', 'question',)
    search_fields = 'choice_text'

报告说:

search\u fields
属性应该是模型上文本类型字段的名称列表

您正在将字符串分配给两个
搜索\u字段
变量。将其更改为列表或元组,如下所示:

search_fields = ('question_text',)
search_fields = ('choice_text',)

非常感谢你!我是个白痴哈哈。我感谢你的帮助!