0

鉴于以下来自美味派的 JSON 结果,我想在 check.payments_total 创建一个新值,该值等于付款总额(在本例中为 44.00)。关于如何做到这一点的任何线索?我完全被难住了。付款是检查表的连接外键。

{
"objects": [
    {
        "check": {
            "id": "58a81b36-1ea6-403b-9902-a50cbd13cf2e",
            "number": 2,
            "payments": [
                {
                    "amount": "5.00",
                },
                {
                    "amount": "39.00",
                }
            ]
        }
    }
]
}
4

1 回答 1

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 }

你应该完成了。

于 2012-09-11T20:19:29.983 回答