[英]Allow null values in a field in flask-restx / flask-restplus
I am using flask-restx for building an API.我正在使用 flask-restx 来构建 API。
My api model is like the following:我的 api 模型如下所示:
myModel = api.model(
'myModel',
{
'id' : fields.Integer(min=1, required=True),
'code' : fields.String(enum=["A", "B", "C"], required=False),
}
)
By doing so, code cannot be null.通过这样做,代码不能为空。
But sometimes, the code field is null.但有时,代码字段为空。 If it is not, it must be one of the
A
, B
or C
values.如果不是,则它必须是
A
、 B
或C
值之一。
I cannot add None
to the enum list because it is not a string.我无法将
None
添加到枚举列表中,因为它不是字符串。
How to make it possible to be null ?如何使其成为 null ?
add in your script在你的脚本中添加
class NullableString(fields.String):
__schema_type__ = ['string', 'null']
__schema_example__ = 'nullable string'
Then然后
myModel = api.model(
'myModel',
{
'id' : fields.Integer(min=1, required=True),
'code' : NullableString(enum=["A", "B", "C"], required=False),
}
)
This should allow your field to be null这应该允许您的字段为空
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.