2

I am using flask_restplus swagger in flask for api documentation. I want to create a model definition for an api that post data to the database. My problem is that, data is in array form. I am sending data in following format with post request.

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

How we can define model for this type of structure? I am sending data in body.

Lama Madan
  • 617
  • 1
  • 10
  • 22
  • What have you tried so far, and where did you get stuck? – Helen Jan 19 '19 at 08:47
  • order = api.model('Order', { "user_id" : fields.Integer, "product" : { "product_id" : fields.String, "total_price" : fields.Integer, "quantity": fields.Integer } }) – Lama Madan Jan 19 '19 at 10:56

1 Answers1

0

If you are still looking for an answer, follow the following link for help https://github.com/noirbizarre/flask-restplus/issues/18

I had the similar issue i used this link to solve my problem

Part of solution may be this help

{"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

for "user_id" i will use @api.extend to extend my model and try, since i have not tried this so i cannot comment, you need to check this part and update

niharika Singhal
  • 61
  • 1
  • 1
  • 10