1👍
✅
You can count with:
from django.db.models import Count, Q
Foo.objects.annotate(
common_count=Count(
'bars',
filter=Q(bars__foo=main_object)
)
)
or if you only want to retrieve objects that have at least one Bar
in common:
Foo.objects.filter(
bars__foo=main_object
).distinct()
Source:stackexchange.com