我正在使用flask-restx来构建API。
我的api模型如下:
myModel = api.model(
'myModel',
{
'id' : fields.Integer(min=1, required=True),
'code' : fields.String(enum=["A", "B", "C"], required=False),
}
)这样,代码就不能为空。
但有时,代码字段为空。如果不是,则必须是A、B或C值之一。
我无法将None添加到枚举列表,因为它不是字符串。
如何使为null成为可能?
发布于 2020-08-20 16:05:14
在脚本中添加
class NullableString(fields.String):
__schema_type__ = ['string', 'null']
__schema_example__ = 'nullable string'然后
myModel = api.model(
'myModel',
{
'id' : fields.Integer(min=1, required=True),
'code' : NullableString(enum=["A", "B", "C"], required=False),
}
)这应该允许您的字段为空
https://stackoverflow.com/questions/60522242
复制相似问题