django-rest-framework-xml
django-rest-framework-xml copied to clipboard
Feature list item
The Problem
We using this library to render XML in django rest framework. Of course the consumer are ERP system. For this reason the behavior is not useful with list-item.
Sample
{
"creation_date": "2017-07-01 14:00:00",
"orderId": 1,
"positions": [
{
"posNo": 1,
"amount": 3,
"messages": [
{
"type": "O",
"code": "xyz"
},
{
"type": "L",
"code": "zyx"
}
]
},
{
"posNo": 2,
"amount": 1,
"messages": [
{
"type": "O",
"code": "xyz"
},
{
"type": "L",
"code": "zyx"
}
]
}
]
}
std. Renderer
<?xml version="1.0" encoding="utf-8"?>
<root>
<creation_date>2017-07-01 14:00:00</creation_date>
<orderId>1</orderId>
<positions>
<list-item>
<posNo>1</posNo>
<amount>3</amount>
<messages>
<list-item>
<type>O</type>
<code>xyz</code>
</list-item>
<list-item>
<type>L</type>
<code>zyx</code>
</list-item>
</messages>
</list-item>
<list-item>
<posNo>2</posNo>
<amount>1</amount>
<messages>
<list-item>
<type>O</type>
<code>xyz</code>
</list-item>
<list-item>
<type>L</type>
<code>zyx</code>
</list-item>
</messages>
</list-item>
</positions>
</root>
Solution
override <list-item>
tag with parent name (parent_name[0:-1]) without the last sign.
- positions -> position
- messages -> message
class OrderXMLRenderer(XMLRenderer):
root_tag_name = 'order'
override_item_tag_name = True
<?xml version="1.0" encoding="utf-8"?>
<order>
<creation_date>2017-07-01 14:00:00</creation_date>
<orderId>1</orderId>
<positions>
<position>
<posNo>1</posNo>
<amount>3</amount>
<messages>
<message>
<type>O</type>
<code>xyz</code>
</message>
<message>
<type>L</type>
<code>zyx</code>
</message>
</messages>
</position>
<position>
<posNo>2</posNo>
<amount>1</amount>
<messages>
<message>
<type>O</type>
<code>xyz</code>
</message>
<message>
<type>L</type>
<code>zyx</code>
</message>
</messages>
</position>
</positions>
</order>
@jpadilla What is missing or does not fit?
I'd also like to use this
@jpadilla don't you want to use that?
vote to merge this