Reputation: 383
i want to get the count of the user id of this table according to the columns approval_transaction_type and approval_type.
the expected result of this would be.
Approval Transaction Type ID (60)
My current code to achieve is this, but it overwrites the sub list and returns the incorrect result(Which i don't understand why the last array will overwrite all the array):
for transaction in transaction_types:
# Initial Array
transaction["approval_types"] = []
for approval_type in approval_types:
# Get Count of Users
approval_type["count"] = Model.objects.filter(approval_transaction_type=transaction['id'],approval_type=approval_type['id']).values().count()
# Assign this sub list to main list
transaction["approval_types"].append(approval_type)
How do i get the count without looping and use the queryset? Let me know if something is not clear about this. Thanks!
Upvotes: 0
Views: 118
Reputation: 15370
It can be done in one query. Based on this Django equivalent for count and group by
Model.objects.values('approval_type').annotate(Count('user'))
Upvotes: 2