Django-tastypie - how to create a custom xml

I have a ModelResource in tastypie for a simple model with ID and name.

The XML result is shown below. But I would like to use my model name instead of "object". I seem to be endlessly fighting how to solve this - any quick help on this? Thank you, a million, really!

<response>
  <objects type="list">
    <object>
      <id>1</id>
      <name>foo1</name>
      <resource_uri> blah </resource_uri>
    </object>
    <object>
      <id>2</id>
      <name>foo2</name>
      <resource_uri> blah </resource_uri>
    </object>
  </objects>
</response>

      

+3


source to share


1 answer


You can try to overload the alter_list_data_to_serialize (...) method of the Resource class. As the doxteria says:

A hook for modifying the list data before serializing it and sending it to the user.

Useful for restructuring / renaming aspects of what will be sent.

So this is the place when you change the "data" which is converted to XML shortly thereafter and sent to the user.



Something like this should catch you:

def alter_list_data_to_serialize(self, request, data):
    #self.object_class.lower() is the name you'd like instead of "objects"
    data = { self.object_class.lower() : [ element for element in data[ 'objects' ] ] }
    return data

      

I have not tested this, but I am using this method to change how my answers appear to be successful.

+3


source







All Articles