[Fixed]-Filter queryset returned from ManyRelatedManager

1👍

Read about conditional aggregation.

from django.db.models import Sum, Case, When, IntegerField

Article.objects.annotate(
    total_readership=Sum(
        Case(
            When(published_in__is_digital=True, then=1),
            output_field=IntegerField()
        )
    )
)

Leave a comment