how to implement abstract model in spyne

 ̄綄美尐妖づ 提交于 2019-12-25 04:42:36

问题


I need to implement an abstract model using Spyne.

In fact, let's say - as a simple example - that I want to manage a garage business.

I then have the following classes:

class Vehicle(ComplexModel):
     ''' this class is abstract '''
     _type_info = [
         ('owner',Unicode)
     ]

class Car(Vehicle):
    _type_info = [
        ('color',Unicode),
        ('speed',Integer)
    ]

class Bike(Vehicle):
    _type_info = [
        ('size',Integer)
    ]

class Garage(ComplexModel):

    _type_info = [
        ('vehicles',Array(Vehicle))
    ]

When I want to get all vehicles managed by my garage, I will only get their Vehicle properties (aka owner here), and not the other ones.

Is there a way to manage abstract objects with Spyne?

Of course, a simple approach would be to have:

class Garage(ComplexModel):

    _type_info = [
        ('bikes',Array(Bike)),
        ('cars',Array(Car))
    ]

but I don't like it: if I do that, I'll have to change my "Garage" class everytime I create a new Vehicle class... I want my Garage class to manage Vehicles, no matter what type of Vehicles it is. Is it possible?


回答1:


With Spyne 2.12.1-beta, output polymorphism should work (when enabled for the output protocol) with the Array(Vehicle) syntax.

Please see working example here: https://github.com/arskom/spyne/blob/a2d0edd3be5bc0385548f5212b7b4b6d674fd610/examples/xml/polymorphic_array.py



来源:https://stackoverflow.com/questions/31476642/how-to-implement-abstract-model-in-spyne

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!