使用 Flask-RESTPlus 时如何接受字符串类型字段的 None
How to accept None for String type field when using Flask-RESTPlus
我刚开始使用 flask-restplus 进行开发,我不是母语人士,
但我会尽可能清楚地描述我的问题。
我知道flask中有一个fields
模块可以帮助我们定义和过滤响应数据类型,
例如字符串、整数、列表等。
有什么方法可以在使用字段模块时允许 NULL / None?
下面是我用field模块抓取值的代码,
add_group = api.model(
"add_group",
{"team_groups": fields.List(fields.Nested(api.model("team_groups", {
"name": fields.String(example="chicago bulls", description="name of add group"),
"display_name": fields.String(example="bulls", description="display name of add group")})))})
如果display_name
的数据类型不是String,就会出现下面的错误,
{
"errors": {
"team_groups.0.display_name": "123 is not of type 'string'"
},
"message": "Input payload validation failed"
}
我要的是在输入display_name的时候,可以输入bulls
或者None
参考资料/问题好像很少,只找到one result相关
我的问题,但最终转换为非空值以解决问题。
如果我的问题有哪一部分不是很清楚,
请告诉我,谢谢。
以下是我的开发环境:
flask-restplus 0.13.0
Python 3.7.4
postman 7.18.1
以下是我更新后的代码:
from flask_restplus import Namespace, fields
class NullableString(fields.String):
__schema_type__ = ['string', 'null']
__schema_example__ = 'nullable string'
class DeviceGroupDto:
api = Namespace("device/group", description="device groups")
header = api.parser().add_argument("Authorization", location="headers", help="Bearer ")
get_detail_group = api.model(
"getdetail",
{"team_groups": fields.List(fields.String(required=True,
description="team group id to get detail", example=1))})
add_group = api.model(
"add_group",
{"team_groups": fields.List(fields.Nested(api.model("team_groups", {
"name": fields.String(example="chicago bulls", description="name of add group"),
"display_name": NullableString(attribute='a')})))})
如果我输入以下 payload: (by postman)
{
"team_groups": [
{
"name": "chicago bulls",
"display_name": null
}
]
}
还是returns:
{
"errors": {
"team_groups.0.display_name": "None is not of type 'string'"
},
"message": "Input payload validation failed"
}
如果您的某些字段是可选的,则 required=False
add_group = api.model(
"add_group",
{"team_groups": fields.List(fields.Nested(api.model("team_groups", {
"name": fields.String(example="chicago bulls", description="name of add group"),
"display_name": fields.String(example="bulls", description="display name of add group", required=False)})))})
是的,您可以创建一个子 class 并使用它来代替默认的,它也将接受 None
class NullableString(fields.String):
__schema_type__ = ['string', 'null']
__schema_example__ = 'nullable string'
所以你的代码看起来像
{ "property": NullableString(attribute=value)}
另外可以访问issue github.com/noirbizarre/flask-restplus/issues/179
我刚开始使用 flask-restplus 进行开发,我不是母语人士,
但我会尽可能清楚地描述我的问题。
我知道flask中有一个fields
模块可以帮助我们定义和过滤响应数据类型,
例如字符串、整数、列表等。
有什么方法可以在使用字段模块时允许 NULL / None?
下面是我用field模块抓取值的代码,
add_group = api.model(
"add_group",
{"team_groups": fields.List(fields.Nested(api.model("team_groups", {
"name": fields.String(example="chicago bulls", description="name of add group"),
"display_name": fields.String(example="bulls", description="display name of add group")})))})
如果display_name
的数据类型不是String,就会出现下面的错误,
{
"errors": {
"team_groups.0.display_name": "123 is not of type 'string'"
},
"message": "Input payload validation failed"
}
我要的是在输入display_name的时候,可以输入bulls
或者None
参考资料/问题好像很少,只找到one result相关
我的问题,但最终转换为非空值以解决问题。
如果我的问题有哪一部分不是很清楚,
请告诉我,谢谢。
以下是我的开发环境:
flask-restplus 0.13.0
Python 3.7.4
postman 7.18.1
以下是我更新后的代码:
from flask_restplus import Namespace, fields
class NullableString(fields.String):
__schema_type__ = ['string', 'null']
__schema_example__ = 'nullable string'
class DeviceGroupDto:
api = Namespace("device/group", description="device groups")
header = api.parser().add_argument("Authorization", location="headers", help="Bearer ")
get_detail_group = api.model(
"getdetail",
{"team_groups": fields.List(fields.String(required=True,
description="team group id to get detail", example=1))})
add_group = api.model(
"add_group",
{"team_groups": fields.List(fields.Nested(api.model("team_groups", {
"name": fields.String(example="chicago bulls", description="name of add group"),
"display_name": NullableString(attribute='a')})))})
如果我输入以下 payload: (by postman)
{
"team_groups": [
{
"name": "chicago bulls",
"display_name": null
}
]
}
还是returns:
{
"errors": {
"team_groups.0.display_name": "None is not of type 'string'"
},
"message": "Input payload validation failed"
}
如果您的某些字段是可选的,则 required=False
add_group = api.model(
"add_group",
{"team_groups": fields.List(fields.Nested(api.model("team_groups", {
"name": fields.String(example="chicago bulls", description="name of add group"),
"display_name": fields.String(example="bulls", description="display name of add group", required=False)})))})
是的,您可以创建一个子 class 并使用它来代替默认的,它也将接受 None
class NullableString(fields.String):
__schema_type__ = ['string', 'null']
__schema_example__ = 'nullable string'
所以你的代码看起来像
{ "property": NullableString(attribute=value)}
另外可以访问issue github.com/noirbizarre/flask-restplus/issues/179