如何使用字符串列表记录flask-restplus响应

use*_*740 9 python swagger flask-restplus

我正在记录我的响应模型,并且需要显示 api 返回字符串列表。

["user1","user2"]
Run Code Online (Sandbox Code Playgroud)

但模型需要字典(json)格式,如下:

ns.response(200,'Success', NS.model("my_get_model",[{
    "name": fields.String(example="user1"),
}]))
Run Code Online (Sandbox Code Playgroud)

我试过以下代码,但没有一个工作:

ns = Namespace('My Apis')


ns.response(200,'Success', [ns.model("my_get_model",
    fields.String(example="user1")
)])
Run Code Online (Sandbox Code Playgroud)

或者

ns.response(200,'Success', ["user1"])
Run Code Online (Sandbox Code Playgroud)

或者

ns.response(200,'Success', ns.model("my_get_model",fields.List(fields.String(example="user1"))))
Run Code Online (Sandbox Code Playgroud)

请指教。

Pin*_*ang 5

我认为这就是您所需要的:https ://github.com/python-restx/flask-restx/issues/65

尝试简单地传递fields.List而不包装模型。

ns.response(200, 'Success', fields.List(fields.String(example="user1")))
Run Code Online (Sandbox Code Playgroud)

仅供参考,flask-restx它是 的一个分支flask-restplus,它应该具有更多更新的功能,因为flask-restplus由于开发人员无法联系flask-restplus项目所有者而不再维护。


Axe*_*aya 0

我认为你的模型结构是错误的,你需要这样写:

ns.model("my_get_model",{
    "name": fields.String(example="user1")
})
Run Code Online (Sandbox Code Playgroud)

更新: 显示一个列表,例如您需要使用的 JSON 中的键值,fields.List而不是fields.String

ns.model("my_get_model",{
    "name": fields.List(example="user1")
})
Run Code Online (Sandbox Code Playgroud)