views:

167

answers:

1

Hi,

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.

+2  A: 

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()
istruble