Django Rest Framework 更改根项和列表项元素名称
Django Rest Framework change root item and list item element name
我是 Django-Rest 框架的新手。我想根据模型名称更改根和列表项元素名称:
发件人:
<root>
<list-item>worrier1</list-item>
<list-item>worrier2</list-item>
<root>
收件人:
<tests>
<test>worrier1</test>
<test>worrier2</test>
<tests>
我已经覆盖了基于 answer from here 的 XMLRenderer,但我不确定如何将它路由到我的视图。
我的看法:
class SampleViewSet(viewsets.ModelViewSet):
myrenderer = ModifiedXMLRenderer(item_tag="tests", root_tag="test")
queryset = Sample.objects.all()
serializer_class = SampleSerializer
permission_classes = [IsAuthenticatedOrReadOnly]
修改后的 XMLRenderer:
class ModifiedXMLRenderer(XMLRenderer):
def __init__(self, item_tag=None,root_tag=None):
self.item_tag_name = item_tag or "item"
self.root_tag_name = root_tag or "channel"
def render(self, data, accepted_media_type=None, renderer_context=None):
"""
Renders `data` into serialized XML.
"""
if data is None:
return ''
stream = StringIO()
xml = SimplerXMLGenerator(stream, self.charset)
xml.startDocument()
xml.startElement(self.root_tag_name, {})
self._to_xml(xml, data)
xml.endElement(self.root_tag_name)
xml.endDocument()
return stream.getvalue()
def _to_xml(self, xml, data):
if isinstance(data, (list, tuple)):
for item in data:
xml.startElement(self.item_tag_name, {})
self._to_xml(xml, item)
xml.endElement(self.item_tag_name)
elif isinstance(data, dict):
for key, value in six.iteritems(data):
xml.startElement(key, {})
self._to_xml(xml, value)
xml.endElement(key)
elif data is None:
# Don't output any value
pass
else:
xml.characters(force_text(data))
我们可以通过覆盖方法get_renderers
.
来实现它
class SampleViewSet(viewsets.ModelViewSet):
queryset = Sample.objects.all()
serializer_class = SampleSerializer
permission_classes = [IsAuthenticatedOrReadOnly]
def get_renderers(self):
return [ModifiedXMLRenderer(item_tag="tests", root_tag="test")]
我是 Django-Rest 框架的新手。我想根据模型名称更改根和列表项元素名称:
发件人:
<root>
<list-item>worrier1</list-item>
<list-item>worrier2</list-item>
<root>
收件人:
<tests>
<test>worrier1</test>
<test>worrier2</test>
<tests>
我已经覆盖了基于 answer from here 的 XMLRenderer,但我不确定如何将它路由到我的视图。
我的看法:
class SampleViewSet(viewsets.ModelViewSet):
myrenderer = ModifiedXMLRenderer(item_tag="tests", root_tag="test")
queryset = Sample.objects.all()
serializer_class = SampleSerializer
permission_classes = [IsAuthenticatedOrReadOnly]
修改后的 XMLRenderer:
class ModifiedXMLRenderer(XMLRenderer):
def __init__(self, item_tag=None,root_tag=None):
self.item_tag_name = item_tag or "item"
self.root_tag_name = root_tag or "channel"
def render(self, data, accepted_media_type=None, renderer_context=None):
"""
Renders `data` into serialized XML.
"""
if data is None:
return ''
stream = StringIO()
xml = SimplerXMLGenerator(stream, self.charset)
xml.startDocument()
xml.startElement(self.root_tag_name, {})
self._to_xml(xml, data)
xml.endElement(self.root_tag_name)
xml.endDocument()
return stream.getvalue()
def _to_xml(self, xml, data):
if isinstance(data, (list, tuple)):
for item in data:
xml.startElement(self.item_tag_name, {})
self._to_xml(xml, item)
xml.endElement(self.item_tag_name)
elif isinstance(data, dict):
for key, value in six.iteritems(data):
xml.startElement(key, {})
self._to_xml(xml, value)
xml.endElement(key)
elif data is None:
# Don't output any value
pass
else:
xml.characters(force_text(data))
我们可以通过覆盖方法get_renderers
.
class SampleViewSet(viewsets.ModelViewSet):
queryset = Sample.objects.all()
serializer_class = SampleSerializer
permission_classes = [IsAuthenticatedOrReadOnly]
def get_renderers(self):
return [ModifiedXMLRenderer(item_tag="tests", root_tag="test")]