AttributeError:'XMLParser'对象没有属性'charset'

时间:2014-06-08 15:59:12

标签: python xml-parsing django-views django-rest-framework

我的项目中有以下视图:

class MyView(ListAPIView):
    serializer_class = MySerializer
    queryset = MyModel.objects.all()
    paginate_by = 100
    filter_class = MyFilter
    renderer_classes = (XMLParser, )

当我发出get请求时,我收到此错误:

AttributeError :'XMLParser' object has no attribute 'charset'

可能是什么问题。请帮忙。

1 个答案:

答案 0 :(得分:0)

您正在向XMLParser元组添加renderer_classes。 Django Rest Framework不知道如何处理它。相反,您应该将XML 渲染器添加到renderer_classes而不是XML 分析器

class MyView(ListAPIView):
    serializer_class = MySerializer
    queryset = MyModel.objects.all()
    paginate_by = 100
    filter_class = MyFilter
    renderer_classes = (XMLRenderer, )

如果您打算添加XML Parser ,则需要设置parser_classes元组

class MyView(ListAPIView):
    serializer_class = MySerializer
    queryset = MyModel.objects.all()
    paginate_by = 100
    filter_class = MyFilter
    parser_classes = (XMLParser, )