[英]Allow null values in a field in flask-restx / flask-restplus
我正在使用 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 ?
在你的腳本中添加
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),
}
)
這應該允許您的字段為空
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.