本文介绍了flask_restplus'marshal_with返回带有空值的响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用flask_restplus创建文档化的API. 元帅装饰器控制在响应中实际呈现哪些数据,但是我在渲染数据时遇到麻烦.我有以下代码:
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)
然后,当我转到路线/kpis/cliente/mes/ano时,它将返回以下内容:
And then when I go to the route /kpis/cliente/mes/ano it returns this:
{
"cpl_diferencia": null,
"cpl_anterior": null,
"cpl_actual": null
}
为什么将值返回为null?有人可以帮我吗!
Why are the values being returned as null ? can someone help me please!
推荐答案
使用"marshal_with"时无需进行jsonify
You don't need to jsonify when using "marshal_with"
只需返回数据"
return data
=)
这篇关于flask_restplus'marshal_with返回带有空值的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!