Julien Greard
Julien Greard

Reputation: 989

how to implement abstract model in 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.

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?

Upvotes: 0

Views: 207

Answers (1)

Burak Arslan
Burak Arslan

Reputation: 8001

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

Upvotes: 1

Related Questions