0x00F 0x00F - 6 months ago 21
Python Question

How to properly get spyne polymorphic tagnames

Is there a way to get the specific class tag names from my spyne class definitions?

Given the case, my class hierarchy looks like this:

# models.py
class Vehicle(ComplexModel):

class Car(Vehicle):

class Bike(Vehicle):

Then I followed the instructions in the answers of this questions, cause their problem seems similar, to get the polymorphism working.

how to implement abstract model in spyne

How do you @rpc _returns polymorphic types in spyne?

Important code lines which were changed:

# start.py

So far, so good. After i did this, i've got the following response:

<!-- Response.xml -->
<Vehicle xsi:type="Car">

So my question is:

Can i get rid of the general class name
as the tag name and get the name of the specific class e.g.
as tag name instead?

Therefore the response should be something like this:


I've seen some approaches with the `ctx.out_string" and some text replacement in this question:

Remove the namespace from Spyne response variables

Which I think probably can work out fine for me, but if there is a "conventional" way in the API, which I did not find yet, I would prefer to get known of it to use it instead.


This is how XML polymorphism is supposed to work. libxml2's schema validator (that you use through lxml) should interpret it properly.

OTOH, If you are sure you want distinction solely through tag names, have a look at the <choice> tag: http://w3.org/TR/xmlschema-1/#declare-contentModel

Also see an article comparing the two approaches: http://ibm.com/developerworks/library/ws-tip-xsdchoice

You seem to have already figured out how to use polymorphism in Spyne. So here's how you use the <choice> tag: https://github.com/arskom/spyne/blob/57ef5c0db51cb194353c67d317990fe89bc4177d/spyne/test/interface/test_xml_schema.py#L51