简体   繁体   English

Flask Restx 中的字典类型 api.model

[英]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.

相关问题 Flask-Restx Api.model(strict=True) 允许未指定的参数 - Flask-Restx Api.model(strict=True) allowing unspecified params 使用 Flask-RestX 制作 RESTful API 时遇到问题:“规范中没有定义操作!” 和“404” - Having Trouble Making a RESTful API with Flask-RestX: “No operations defined in spec!” and “404”s Flask-Restx(/Flask-Restplus) 检测重载 - Flask-Restx(/Flask-Restplus) detect reload 在命名空间实例化 flask_restx 上添加 doc 装饰器 - Add doc decorators on Namespace instantiation flask_restx 如何测试在 flask/flask-restx 中获取解析器输入的 GET 方法? - How to test the GET method that takes parser inputs in flask/flask-restx? Flask-RESTful API不会以json格式返回字典 - Flask-RESTful API does not return a dictionary as a json format Python/Django 模型字典允许一种类型的更新,但不允许另一种类型的更新 - Python/Django model dictionary allows one type of update, but not another 如何通过 Flask 中的 API 检查来自 POST 请求的数据类型? 如果传入的数据不是图像,则引发错误 - How to check the type of data coming from POST request through API in Flask? Raising an error if incoming data is not is not an Image Flask 长轮询 API - Flask Long Polling API 在 Flask API 中模拟一个类 - Mocking a class in a Flask API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM