Object of type 'Decimal' is not JSON serializable
转载自: https://blog.csdn.net/weixin_41951954/article/details/124838931
报这个错是因为json.dumps函数发现字典里面有 Decimal类型的数据,无法JSON serializable
解决方法:是检查到Decimal类型的值转化成float类型
代码(已测,ok)
class DecimalEncoder(json.JSONEncoder): def default(self, o): if isinstance(o, decimal.Decimal): return float(o) super(DecimalEncoder, self).default(o)
def deal_response(code=200, msg='', data=None, cls=None):
if data is None:
data = []
ret = {
'code': code,
'msg': msg,
'data': data,
}
response = HttpResponse(json.dumps(ret, cls=cls))
return response
图