1👍
✅
You can aggregate over all the fields, so:
UserTransaction.objects.aggregate(
total_deposit=Sum('is_deposit'),
total_withdrawal=Sum('is_withdrawal'),
total_interest=Sum('is_interest'),
total_investment=Sum('is_investment'),
total_return=Sum('is_return')
)
or for some databases where a bool is not implemented as an integer:
UserTransaction.objects.aggregate(
total_deposit=Count('pk', filter=Q(is_deposit=True)),
total_withdrawal=Count('pk', filter=Q(is_withdrawal=True)),
total_interest=Count('pk', filter=Q(is_interest=True)),
total_investment=Count('pk', filter=Q(is_investment=True)),
total_return=Count('pk', filter=Q(is_return=True))
)
This will return a dictionary that looks like:
{
'total_deposit': 14,
'total_withdrawal': 25,
'total_interest': 13,
'total_investment': 2
'total_return': 17
}
Source:stackexchange.com