Reputation: 11561
is there a way to do the following in one Django query?
MyModel.filter(attr=a).values('attr','fields','of','interest').annotate(count=Count('id'))
MyModel.filter(attr=b).values('attr','fields','of','interest').annotate(count=Count('id'))
edit:
I need separate counts for a and b. MyModel.filter(attr__in=(a,b))...
or MyModel.filter(Q(attr=a)|Q(attr=b))...
won't work I guess.
Upvotes: 1
Views: 1017
Reputation: 13692
Your MyModel class may have an order_by value set to order on something not in ('attr','fields','of','interest'). Try removing the ordering or ordering by one or more of the fields of interest.
MyModel.objects.values('attr','fields','of','interest'
).annotate(count=Count('id')).order_by()
Upvotes: 4