Django-Rest-Framework,如何从结果中删除元数据

时间:2015-03-24 12:45:19

标签: django-rest-framework

我为JQuery UI Autocomplete设置了ListApiView。 它正确过滤,但结果包含一些元数据

{
    "count": 710, 
    "next": "http://127.0.0.1:8000/taxonomy/lookup/?term=hom&page=3", 
    "previous": "http://127.0.0.1:8000/taxonomy/lookup/?term=hom&page=1", 
    "results": [
        {
            "label": "Homaledra sabalella", 
            "value": "Homaledra sabalella"
        }, 
        {
            "label": "Homaledra sabalella (Chambers, 1880)", 
            "value": "Homaledra sabalella (Chambers, 1880)"
        },
        .....
        ... 
    }
}

我可以在哪里配置它,以便我只返回"结果"清单? 查看文档似乎是渲染器。如何在ListApiView中设置它?

1 个答案:

答案 0 :(得分:2)

这些“元数据”来自PaginationSerializer, 默认list方法将serializer_class包裹到PaginationSerializer

您可以覆盖list方法:

class ViewSet(GenericViewSet): 
    def list(self, request, *args, **kwargs):
        queryset = self.filter_queryset(self.get_queryset())
        # If you want response all the results, without pagination, 
        # stop calling the self.paginate_queryset method, use queryset directly
        page = self.paginate_queryset(queryset) or queryset
        serializer = self.get_serializer(page, many=True)
        return Response(serializer.data)