Python JSON serialize a Decimal object

后端 未结 17 1184
星月不相逢
星月不相逢 2020-11-22 08:27

I have a Decimal(\'3.9\') as part of an object, and wish to encode this to a JSON string which should look like {\'x\': 3.9}. I don\'t care about p

17条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-22 09:10

    For anybody that wants a quick solution here is how I removed Decimal from my queries in Django

    total_development_cost_var = process_assumption_objects.values('total_development_cost').aggregate(sum_dev = Sum('total_development_cost', output_field=FloatField()))
    total_development_cost_var = list(total_development_cost_var.values())
    
    • Step 1: use , output_field=FloatField() in you r query
    • Step 2: use list eg list(total_development_cost_var.values())

    Hope it helps

提交回复
热议问题