我想使用漏勺制作的模型中的某些字段是可选的
我熟悉使用XPST0008: Variable myParam has not been declared (or its declaration is not in scope)
,但只有在定义SchemaNode时才有效
如果使用自定义类(例如missing=colander.drop
)定义字段,如何将其设为可选字段?
以下是摘录:
customeClass = CustomClass()
答案 0 :(得分:1)
为了使自定义Class对象成为可选,我们可以传递相同的missing=colander.drop
作为构造函数参数。
示例:强>
import colander
class Image(colander.MappingSchema):
url = colander.SchemaNode(colander.String())
width = colander.SchemaNode(colander.Int())
height = colander.SchemaNode(colander.Int())
class Post(colander.MappingSchema):
id = colander.SchemaNode(colander.Int())
text = colander.SchemaNode(colander.String())
score = colander.SchemaNode(colander.Int())
created_time = colander.SchemaNode(colander.Int())
attachedImage = Image(missing=colander.drop) # The difference