鉴于以下来自tastypie的JSON结果,我想在check.payments_total创建一个新值,该值等于付款总额(在本例中为44.00)。有关如何做到这一点的任何线索?我完全难过了。付款是检查表的加入外键。
{
"objects": [
{
"check": {
"id": "58a81b36-1ea6-403b-9902-a50cbd13cf2e",
"number": 2,
"payments": [
{
"amount": "5.00",
},
{
"amount": "39.00",
}
]
}
}
]
}
答案 0 :(得分:1)
如果对于回复,您可以在资源中覆盖以下方法(代码段来自tastypie.resources.Resource
):
def alter_list_data_to_serialize(self, request, data):
"""
A hook to alter list data just before it gets serialized & sent to the user.
Useful for restructuring/renaming aspects of the what's going to be
sent.
Should accommodate for a list of objects, generally also including
meta data.
"""
return data
只包括类似的内容(未经过测试,认为是伪代码):
total_amount = 0.0
for object in data[ 'objects' ]:
total_amount += object[ 'amount' ]
return { 'objects' : data[ 'objects' ], 'total_amount' : total_amount }
你应该完成。