There are three models A, B, C:
Class A: some_field = models.CharF... Class B: a = models.ForeignKey('A') с = models.ForeignKey('С') Class C some_field = models.CharF... How can Django orm get the number of A objects belonging to C?
For example:
There are objects: A1, A2.
Object: C1.
And connections: B1 (A1, C1), B2 (A1, C1), B3 (A2, C1).
In this example, the number of objects A belonging to C will be 2.
I tried this:
queryset = A.objects.all() queryset.values('b__c').annotate(num_results=Count('b__c')) As a result, I get that the object C1 belongs to 3 objects. Thus, I consider the number of connections that does not suit me.