干草堆 - ' SearchQuerySet'对象没有属性' model'

时间:2016-04-20 15:42:05

标签: python django django-rest-framework django-haystack

我正在尝试序列化haystack结果,

api.py

class PinSearchViewSet(viewsets.ReadOnlyModelViewSet):
    http_method_names = ['get']
    queryset = Pin.objects.all()
    serializer_class = PinSearchSerializer

    def get_queryset(self, *args, **kwargs):
        request = self.request
        queryset = EmptySearchQuerySet()
        if request.GET.get('q') is not None:
            query = request.GET.get('q')
            queryset = SearchQuerySet().filter(content=query)
        return queryset

serializer.py

class PinSearchSerializer(serializers.ModelSerializer):
    class Meta:
        model = Pin
        fields = ('id', 'office_name', 'pin_code', 'office_type', 'delivery_status',
                  'division_name', 'region_name', 'circle_name', 'taluk', 'district', 'state')

但是我收到了以下错误,

  

' SearchQuerySet'对象没有属性' model'

1 个答案:

答案 0 :(得分:1)

序列化程序需要 pin 对象,但您要返回 SearchResut 对象

试试这个

class PinSearchViewSet(viewsets.ReadOnlyModelViewSet):
    http_method_names = ['get']
    queryset = Pin.objects.all()
    serializer_class = PinSearchSerializer

    def get_queryset(self, *args, **kwargs):
        request = self.request
        queryset = EmptySearchQuerySet()
        if request.GET.get('q') is not None:
            query = request.GET.get('q')
            queryset = SearchQuerySet().filter(content=query).models(Pin)
        return [i.object for i in queryset]

我还在 searchQuerySet

中指定了 pin 模型