1

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.

1 Answer 1

4

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.