[Fixed]-Count attributes of many to many relation and filter by it

1👍

You can use annotations for this.

from django.db.models import Count
Party.objects.filter(
    participants__has_iphone=True
).annotate(iphone_count=Count('participants')).filter(
    iphone_count=4
)

Leave a comment