Here is my code
reports: queryset
for reports in reports:
data.append(DataModel(
model_id=report.num,
title=report.title,
total_price=report.total_amount,
))
This code will create some DataModel objects and will append the object into a list.
I want to sum total_price of all the objects with the same obj.id.
For example: If we have these objects on the queryset:
- id:obj1 total_price: 10
- id:obj3 total_price: 20
- id:obj2 total_price: 30
- id:obj1 total_price: 40
- id:obj2 total_price: 50
In the list I want to have these object in the list:
- id:obj1 total_price: 50
- id:obj3 total_price: 20
- id:obj2 total_price: 80
What is the best practice to do that?