flask_restplus' marshal_with returns response with null values

最后都变了- 提交于 2020-01-03 05:17:07

问题


I am using flask_restplus to create a documented API. The marshal decorator controls what data actually gets rendered in your response, but I am having trouble rendering the data. I have the following code:

kpis_model = api.model('kpis', {
    'cpl_actual': fields.Integer(description='costo por lead total del mes actual'),
    'cpl_anterior': fields.Integer(description='costo por lead total del mes anterior'),
    'cpl_diferencia': fields.Integer(description='diferencia de cpl_actual y cpl_anterior')
})

@data.route('/kpis/<cliente>/<mes>/<ano>')
@data.doc(params={'cliente': 'id de Facebook del cliente','mes':'el mes que se desea usar (dos digitos)','ano':'el ano que se desea usar (cuatro digitos)'})
class Kpis(Resource):
    @data.marshal_with(kpis_model)
    def get(self,cliente,mes,ano):
        '''sacar KPIs principales'''
        data = {}
        data['cpl_actual'] = 300
        data['cpl_anterior'] = 100
        data['cpl_diferencia'] = data['cpl_actual'] - data['cpl_anterior']
        return jsonify(data)

And then when I go to the route /kpis/cliente/mes/ano it returns this:

{
   "cpl_diferencia": null,
   "cpl_anterior": null,
   "cpl_actual": null
}

Why are the values being returned as null ? can someone help me please!


回答1:


You don't need to jsonify when using "marshal_with"

Just return "data"

return data

=)



来源:https://stackoverflow.com/questions/48708222/flask-restplus-marshal-with-returns-response-with-null-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!