在ListCreateAPIView中向JSON响应添加其他数据

时间:2018-07-10 18:14:00

标签: django django-rest-framework

class ItemListView(ListCreateAPIView):
    model = Item
    serializer_class = ItemSerializer # model serializer

    def get_queryset(self):
        return self.model.objects.all()

    def get(self, request, *args, **kwargs):
        return self.list(request, *args, **kwargs)

是否有机会通过get方法向JSON响应中添加其他数据?

2 个答案:

答案 0 :(得分:2)

您可以覆盖ItemSerializer的{​​{1}}方法,如

to_representation()

答案 1 :(得分:0)

response = self.list(..)
response.data['hello'] = 'world'
return response