本文介绍了flask_restplus' marshal_with 返回带有空值的响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 flask_restplus 创建一个文档化的 API.marshal 装饰器 控制响应中实际呈现的数据,但是我在呈现数据时遇到问题.我有以下代码:
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 返回带有空值的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!