I have model declared as:

class SAProduct(Base):

    sku = Column(PRODUCT_SKU_TYPE, primary_key=True)
    i_want_to_hide = Column(String(20), nullable=False)
    name = Column(Unicode(255), nullable=True)

    @property
    def my_property(self):
        return i_calculate_property_here(self)

and Spyne model declared as:

db = create_engine('sqlite:///:memory:')
Session = sessionmaker(bind=db)

class TableModel(ComplexModelBase):
    __metaclass__ = ComplexModelMeta
    __metadata__ = MetaData(bind=db)

class SProduct(TableModel):
    __table__ = SAProduct.__table__

How can I make attribute i_want_to_hide to be excluded from Spyne model, and property my_property to be included as Spyne model attribute?

P.S. Now I use monkey patching Spyne to support this syntax:

class SProduct(GComplexModel):
    __model__ = Product

    class Attributes:
        exclude_attrs = ('i_want_to_hide',)
        add_attrs = {'my_property': Boolean}

But I want get rid of it.

1

There are 1 best solutions below

2
On

This doesn't directly answer your question, but please consider the following code:

from spyne import *

TableModel = TTableModel()

class SomeClass(TableModel):
    __tablename__ = 'some_table'

    id = Integer(pk=True)
    s = Unicode
    i = Integer(exc_table=True)

Here, pk stands for primary key (you can use the long form primary_key if you wish) and the i attribute will just be ignored by SqlAlchemy. e.g. it won't be created in the table, it won't be instrumented by SqlAlchemy's metaclass, etc.

As for an attribute that will be hidden from the RPC Parts of Spyne, but not from SqlAlchemy, that's a new feature coming in 2.12.

You will be able to say e.g.:

i = Integer(exc_table=True, pa={JsonObject: dict(exc=true)})

where pa stands for protocol attributes. (you can use the long form prot_attrs if you wish) Here i is ignored by every protocol that inherits JsonObject.

If you don't want it on the wsdl either, you'll have to do:

i = Integer(exc_table=True, exc_interface=True)

https://github.com/arskom/spyne/blob/fa4b1eef5815d3584287d1fef66b61846f82d2f8/spyne/interface/xml_schema/model.py#L197

Spyne offers a richer object model interface compared to SqlAlchemy. Trying to replicate this functionality without adding Spyne as a dependency means you'll have to duplicate all the work done in Spyne in your project. It's your choice!