django-rest-framework-xml icon indicating copy to clipboard operation
django-rest-framework-xml copied to clipboard

Feature list item

Open philall opened this issue 7 years ago • 4 comments

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>

philall avatar Aug 02 '17 08:08 philall

@jpadilla What is missing or does not fit?

philall avatar Aug 14 '17 13:08 philall

I'd also like to use this

loftsmartmatt avatar Sep 14 '17 18:09 loftsmartmatt

@jpadilla don't you want to use that?

philall avatar Sep 19 '17 08:09 philall

vote to merge this

aijogja avatar Oct 22 '19 02:10 aijogja