Django Rest测试不将XML作为响应而是json

时间:2021-02-16 20:28:04

This is my view

这是我的观点

from rest_framework_xml.renderers import XMLRenderer

@api_view(['GET', 'PUT', 'POST'])
@renderer_classes((XMLRenderer,))
@permission_classes((AllowAny,))
def users(request, id, format=None):
    serializer = MySerializer(model,  context={'request': request})
    return Response(serializer.data)

If i visit the URL by browser then i get XML as response in chrome as desired.

如果我通过浏览器访问URL,那么我会根据需要获取XML作为响应。

But in my tests

但在我的测试中

url = reverse('user-detail', kwargs={
    'id': 10,
})
response = self.client.get(url)
result = response.data

I get the result as dictionary . I mean the result which i get before passing to XMLRender.

我得到的结果是字典。我的意思是在传递给XMLRender之前得到的结果。

I don't know if thats the desired behaviour but how can test the xml response

我不知道这是否是所需的行为,但如何测试xml响应

1 个解决方案

#1


1  

The .data property of the Response holds the unrendered content. You want to access .content to see your rendered XML.

Response的.data属性保存未呈现的内容。您想访问.content以查看您呈现的XML。

#1


1  

The .data property of the Response holds the unrendered content. You want to access .content to see your rendered XML.

Response的.data属性保存未呈现的内容。您想访问.content以查看您呈现的XML。