[Answer]-Django tastypie get related column total amount

1๐Ÿ‘

โœ…

If for the response, then you could override the following method in your resource (the snippet is from 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

just include something like (not tested, consider to be pseudo-code):

 total_amount = 0.0
 for object in data[ 'objects' ]:
     total_amount += object[ 'amount' ]

 return { 'objects' : data[ 'objects' ], 'total_amount' : total_amount }

and you should be done.

๐Ÿ‘คkgr

Leave a comment