首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >python flask-restplus swagger中数组数据的模型定义

python flask-restplus swagger中数组数据的模型定义
EN

Stack Overflow用户
提问于 2019-01-19 09:01:07
回答 1查看 2.6K关注 0票数 2

我在flask中使用flask_restplus swagger获取api文档。我想为将数据发布到数据库的api创建一个模型定义。我的问题是,数据是数组形式的。我正在用post请求发送以下格式的数据。

代码语言:javascript
复制
{
    "user_id" : 3,
    "product" : [
        {
            "product_id" : 33,
            "total_price" : 50,
            "quantity": 2
        },
        {
            "product_id" : 18,
            "total_price" : 40,
            "quantity": 2
        }
    ]
}

我们如何为这种类型的结构定义模型?我在正文中发送数据。

EN

回答 1

Stack Overflow用户

发布于 2019-03-13 17:56:56

如果您仍在寻找答案,请访问以下帮助https://github.com/noirbizarre/flask-restplus/issues/18链接

我有类似的问题,我使用这个链接来解决我的问题

解决方案的一部分可能是这个帮助

代码语言:javascript
复制
{"product" : [
        {
            "product_id" : 33,
            "total_price" : 50,
            "quantity": 2
        },
        {
            "product_id" : 18,
            "total_price" : 40,
            "quantity": 2
        }
    ]
}

order = api.model( "product" : { "product_id" : fields.String, "total_price" : fields.Integer, "quantity": fields.Integer } }) 



@api.route('/somewhere')
class MyAPI(Resource):
    @api.expect([order])
    def post(self):
        pass
or 

@api.route('/somewhere')
class MyAPI(Resource):
    @api.doc(body=[order])
    def post(self):
        pass

对于"user_id“,我将使用@api.extend来扩展我的模型并尝试,因为我还没有尝试过,所以我无法评论,你需要检查这部分并更新

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54263231

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档