[英]Dictionary type api.model in flask Restx
I am new to flask restx and trying to create a dict type api.model but I cannot find any example or documentation where it is mentioned, I am not sure if there is any work around to achieve this.我是 flask restx 的新手并试图创建一个 dict 类型的 api.model 但我找不到任何提到它的示例或文档,我不确定是否有任何解决方法可以实现这一点。 Please share your ideas
请分享你的想法
Here is my data I want to return from the api call这是我想从 api 调用返回的数据
{
"sandbox-iosxe-recomm-1.cisco.com": {
"username": "developer",
"password": "cisco",
"protocol": "ssh",
"port": 22,
"os_type": "iosxe"
},
"sandbox-iosxr-1.cisco.com": {
"username": "admin",
"password": "cisco",
"protocol": "ssh",
"port": 22,
"os_type": "iosxr"
}
Here is the api.model schema I was trying to create but this doesnt work obviously as there is a single key and value and trying to figuring out, how to achieve this.这是我试图创建的 api.model 模式,但这显然不起作用,因为只有一个键和值,并试图弄清楚如何实现这一点。 I do not want to use List for some reason.
出于某种原因,我不想使用 List。
device_model = api.model('Device', {
'device': {
'username': fields.String(required=True, description='username', default='device username'),
'password': fields.String(required=True, description='password', default='device password'),
'protocol': fields.String(required=True, description='protocol', default='protocol name'),
'port': fields.Integer(required=True, description='port', default='port number'),
'os_type': fields.String(required=True, description='os_type', default='device OS type'),
}
}
)
You can use a nested model您可以使用嵌套模型
device_nested = api.model(
"device_nested",
{
'username': fields.String(required=True, description='username', default='device username'),
'password': fields.String(required=True, description='password', default='device password'),
'protocol': fields.String(required=True, description='protocol', default='protocol name'),
'port': fields.Integer(required=True, description='port', default='port number'),
'os_type': fields.String(required=True, description='os_type', default='device OS type'),
},
)
device = api.model(
"device",
{
"device": Nested(device_nested),
},
)
It should result in它应该导致
{
"device": {
...
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.