【问题标题】:Model definition of array data in python flask-restplus swaggerpython flask-restplus swagger中数组数据的模型定义
【发布时间】:2019-01-19 01:01:06
【问题描述】:

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

{
    "user_id" : 3,
    "product" : [
        {
            "product_id" : 33,
            "total_price" : 50,
            "quantity": 2
        },
        {
            "product_id" : 18,
            "total_price" : 40,
            "quantity": 2
        }
    ]
}

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

【问题讨论】:

  • 到目前为止您尝试过什么,您在哪里卡住了?
  • order = api.model('Order', { "user_id" : fields.Integer, "product" : { "product_id" : fields.String, "total_price" : fields.Integer, "quantity ": fields.Integer } })

标签: rest api flask swagger flask-restplus


【解决方案1】:

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

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

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

{"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 扩展我的模型并尝试,因为我没有尝试过,所以我无法评论,你需要检查这部分并更新

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-10
    • 1970-01-01
    • 1970-01-01
    • 2019-07-16
    • 2019-11-04
    相关资源
    最近更新 更多