[Answered ]-Django – Annotate count of related objects

1👍

You annotate with Count [Django-doc]:

from django.db.models import Count


def annotate_count_a(b_queryset):
    return b_queryset.annotate(na=Count('a'))

The B objects that arise from this queryset will then have an extra attribute .na that contains the number of related A records.

Leave a comment