如何在spyne中实现抽象模型 [英] how to implement abstract model in spyne
问题描述
我需要使用Spyne实现抽象模型.
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.
然后我有以下课程:
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.
有没有办法用Spyne管理抽象对象?
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))
]
但我不喜欢它:如果这样做,我每次创建新的Vehicle类时都必须更改"Garage"类...我希望我的Garage类可以管理Vehicles,无论哪种类型的车辆.有可能吗?
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?
推荐答案
对于Spyne 2.12.1-beta,输出多态性应在使用Array(Vehicle)
语法的情况下(在为输出协议启用时)起作用.
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
这篇关于如何在spyne中实现抽象模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!