How can I count related objects in Django (in less than N queries, where N is number of object).
To clarify, let's say I have tables A and B. Every B is connected to exactly one A. Approach I tried:
A.objects.select_related().filter(attr=val)
A[i].B_set.count()
Of course, for every A[i] I want to find out number of B objects Django executes one query.
So the question is - is there a way to optimize that?