Simple Subquery with OuterRef
One of the problems with your example is that you cannot use queryset.count() as a subquery, because .count() tries to evaluate the queryset and return the count. So one may think that the right approach would be to use Count() instead. Maybe something like this: Post.objects.annotate( count=Count(Tag.objects.filter(post=OuterRef(‘pk’))) ) This won’t work for two reasons: The … Read more