sum values in django models

sum_a = sum([item.column for item in queryset]) # Definitely takes more memory.
sum_b = queryset.aggregate(Sum('column')).get('column__sum') # Takes about 20% more time.

Here is what the above code is Doing:
1. The first line is creating a list of all the column values.
2. The second line is using the aggregate function to sum all the column values.
3. The third line is using the aggregate function to sum all the column values.

The first line is the most memory intensive, but it’s also the fastest. The second and third lines are less memory intensive, but they’re also slower.

So, if you’re looking for the fastest way to sum a queryset, use the first method. If you’re looking for the most memory efficient way, use the second or third method.