I have following:
from spyne.service import ServiceBase
from spyne.util import xml
from spyne.model import complex, primitive
class ComplexModel(complex.ComplexModelBase):
__namespace__ = 'http://xml.candyshop.com/ns/candies/'
__metaclass__ = complex.ComplexModelMeta
class CandyModel(ComplexModel):
__type_name__ = 'candy'
flavor = complex.XmlAttribute(primitive.Unicode)
class BagModel(ComplexModel):
__type_name__ = 'bag'
candies = complex.Array(CandyModel)
class CandyShop(ServiceBase):
__tns__ = 'http://xml.candyshop.com/ns/shop/'
@rpc(_returns=primitive.AnyXml)
def get_my_bag(ctx):
bag = BagModel()
bag.candies = [CandyModel(flavor='choco')]
return xml.get_object_as_xml(
bag,
cls=BagModel,
root_tag_name='bag',
)
@classmethod
def dispatch(cls):
from django.views.decorators.csrf import csrf_exempt
from spyne.application import Application
from spyne.server.django import DjangoApplication
application = Application([cls],
tns=cls.__tns__,
in_protocol=Soap11(validator='lxml'),
out_protocol=Soap11(cleanup_namespaces=True)
)
return csrf_exempt(DjangoApplication(application))
shop_service = CandyShop.dispatch()
And result for get_my_bag
is like:
<tns:get_my_bagResult xmlns:tns="http://xml.candyshop.com/ns/shop/">
<ns0:bag xmlns:ns0="http://xml.candyshop.com/ns/candies/">
<ns0:candies>
<ns1:candy xmlns:ns1="None" flavor="choco"/>
</ns0:candies>
</ns0:bag>
</tns:get_my_bagResult>
But I want following:
<tns:get_my_bagResult xmlns:tns="http://xml.candyshop.com/ns/shop/">
<ns0:bag xmlns:ns0="http://xml.candyshop.com/ns/candies/">
<ns0:candies>
<ns0:specialCandy flavor="choco"/>
</ns0:candies>
</ns0:bag>
</tns:get_my_bagResult>
So, how to customize type name for array content without definning new subclass? I tried
complex.Array(CandyModel.customize(type_name='specialCandy'))
but this not works. Using of static alias
method gives an empty <ns0:candies/>
, maybe for that I'm still put CandyModel
instances to the candies
list, but this is my goal.
Second, why there is xmlns:ns1="None"
and how to fix it for ns0
?
BTW. Is there a way to customize namespace prefixes?
EDIT
class Candies(complex.Array):
__namespace__ = 'http://xml.candyshop.com/ns/candies/'
and
candies = Candies(CandyModel)
solves problem with namespaces, but its a workaround rather than solution. I prefer inline customization or some of mixin with my namespaced ComplexModel
.