设置spyne以跳过SOAP请求中不存在的字段?

时间:2013-07-16 12:45:22

标签: python soap spyne

我有一个带有这样一列的SQLALchemy模型:

updated_at = Column(DateTime, nullable=False, server_default=func.now(),
                        onupdate=func.now())

生成WSDL:

<xs:element name="updated_at" type="xs:dateTime" minOccurs="0"/>

在更新请求updated_at字段中缺少,并且spyne将其值映射到None,导致此错误:

IntegrityError: (IntegrityError) null value in column "updated_at" violates not-null constraint
 'UPDATE subcsription SET updated_at=%(updated_at)s WHERE subcsription.id = %(subcsription_id)s' {'subcsription_id': 27, 'updated_at': None}

如果没有在SOAP请求中传递spyne,我怎么能设置spyne呢?

1 个答案:

答案 0 :(得分:1)

您的界面和数据库架构不需要匹配1对1。您可以定义一个没有updated_at成员的单独对象,并将其用作服务定义中的输入类型。 E.g。

class SomeObject(TableModel):
    __tablename__ = 'some_table'

    id = UnsignedInteger64(pk=True)
    updated_at = DateTime(server_default=func.now(), onupdate=func.now())
    some_data = Unicode

class SomeWriteObject(TableModel):
    __tablename__ = 'some_table'

    id = UnsignedInteger64(pk=True)
    some_data = Unicode

# or, you could do this:
class SomeWriteObject(TableModel):
    __tablename__ = 'some_table'
    _type_info = [ (k,v) for k,v in SomeObject._type_info.items()
                                                  if not k in ('updated_at',) ]
class SomeService(ServiceBase):
    @rpc(SomeWriteObject, _returns=UnsignedInteger64)
    def add_some_object(ctx, obj):
        ctx.udc.session.add(obj)
        ctx.udc.session.commit()
        return obj.id

这种方式updated_at将在插入查询中省略,该查询将根据您的指示将其留给数据库以填充该字段。