flask_restplus' marshal_with returns response with null values

2.5k views Asked by At

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

There are 1 answers

1
Carol Serrão On

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

Just return "data"

return data

=)