如何在spyne中默认可选字段?

时间:2015-07-15 19:48:14

标签: optional spyne

这是一些Spyne / SOAP代码,我将返回一个或两个字符串。有没有办法避免必须将string2设置为None表示它不存在?

class TestResult(ComplexModel):
    """
    """
    string1 = Unicode
    string2 = Unicode(min_occurs=0, max_occurs=1)

    def __init__(self):
        # Say hello
        self.string1 = "Hello World"

        # Either of the following works
        #
        # self.string2 = "...from Paul"
        # self.string2 = None
        #
        # But omitting them doesn't.


class Test(ServiceBase):
    """
    """
    @srpc(Unicode, _returns=TestResult)
    def Test(Query):
        """
        """
        response = TestResult()

        return response

2 个答案:

答案 0 :(得分:0)

我真的不明白这个问题,请澄清。

string2如果xsi:null min_occurs=1,则会{{1}}显式返回,如果这就是您所要求的。

答案 1 :(得分:0)

string2是可选的(min_occurs = 0)但我必须将其显式设置为“None”才能完全省略XML响应。我问是否有一些我缺少的东西,允许我编写代码(即string2从未明确设置)并生成有效的响应。 背景是我必须支持一些XML,其中响应有大量的可选字段,但我必须设置一个长列表tom NULL - 所以我希望我可以懒惰并找到一种方法来避免必须这样做; - )。

相关问题