I have a model which has a field named 'state'. It has values like 'completed', 'in_progress', 'failed', 'created'. I need to know if I can write a django query which will through me a list like
{'completed': 2, 'in_progress': 5, 'failed': 0, 'created': 2}
for the table values
id | order_id | state
---------------------------
1 | 23 | completed
2 | 23 | completed
3 | 23 | in_progress
4 | 23 | created
5 | 23 | created
6 | 23 | in_progress
7 | 23 | in_progress
8 | 23 | in_progress
9 | 23 | in_progress
I tried running the below query
order_items = OrderItems.objects.filter(order=order)
order_states = order_items.filter(
state__in=['in_progress', 'completed', 'failed', 'created']
).values('state').annotate(Count('state'))
But it gave me a list which came up like this
[{'state': u'completed', 'state__count': 8},
{'state': u'failed', 'state__count': 1},
{'state': u'in_progress', 'state__count': 1}]