[Answered ]-Query to count reverse manytomany field

1👍

You filter with:

from django.db.models import Count, Q

CustomUser.objects.annotate(
    banned_by_count=Count(
        'user_list', distinct=False, filter=Q(user_list__ban_mode__ban_mode='BAN')
    )
)

otherwise you will filter where the user was the one taking the action, not the "victim".

Leave a comment