1
class TestAPI(APIView):   
    def post(self, request, format=None):
        # Here, I process the request in SomeClass method
        # 'some_result' is a tuple in the format below
        some_result = SomeClass.some_method(request)
        response = Response(some_result, status=status.HTTP_200_OK, headers=None)
        return response

仅供参考,'some_result'包含:

(u'text1', [('dataA1', 'dataA2', 'dataA3'), ('dataB1', 'dataB2', 'dataB3')], 'text2')

所以我的问题是,是否可以解析这个元组(上图),所以我可以像这样返回一个 tsv:

dataA1 dataA2 dataA3
dataB1 dataB2 dataB3

提前谢谢了。

4

1 回答 1

3

查看有关实现自定义渲染器的文档。

http://django-rest-framework.org/api-guide/renderers.html#custom-renderers

自定义渲染器看起来像这样......

class TSVRenderer(renderers.BaseRenderer):
    media_type = 'text/tsv'
    format = 'tsv'

    def render(self, data, media_type=None, renderer_context=None):
        return ...  # TODO: Convert you incoming data representation into a TSV string

然后,您可以通过设置renderer_classes属性将渲染器添加到视图...

class TestAPI(APIView):
    renderer_classes = [TSVRenderer]
    ...
于 2013-02-01T19:37:31.077 回答