2

我是第一次使用SPYNE,我对如何使用我的 ComplexModel 做出响应感到有些困惑。

#service.py
class Status(ComplexModel):
    statusCode = Integer
    statusMsg = String

class ResponseData(ComplexModel):
    status = Array(Status)
    version = Integer

class SoapService(ServiceBase):
    @rpc(String, _returns=ResponseData)
    def doSomething(ctx, name):
        # ...
        # do something to get a django-model-object
        obj_data = Servers.objects.get(host=name)
        # put vars from the django-model-object into 'ResponseData' and nested 'Status'
        # e.g. statusCode = 1, statusMsg = 'online', version = 1

        """ experimental - status is not returned - version is 1:
        obj_status = Status()
        obj_status.statusCode = 1
        obj_status.statusMsg = 'online'

        retval = ResponseData()
        retval.status = obj_status
        retval.version = 1
        """
        return retval

有人知道如何填充 ComplexModels 吗?文档和示例无法帮助我。

4

1 回答 1

5

我找到了解决方案。这里有一个小演示。

#service.py
class Status(ComplexModel):
    statusCode = Integer
    statusMsg = String

class ResponseData(ComplexModel):
    status = Array(Status)
    version = Integer

class SoapService(ServiceBase):
    @rpc(String, _returns=ResponseData)
    def doSomething(ctx, name):

        obj_data = Servers.objects.get(host=name)
        if obj_data.status:
            my_statusCode = 1
            my_statusMsg = 'online'
        else:
            my_statusCode = 0
            my_statusMsg = 'offline'

        retval = ResponseData()
        retval.status = [{'statusCode': my_statusCode, 'statusMsg': my_statusMsg}]
        retval.version = 1
        return retval
于 2012-09-17T15:22:10.523 回答