Thanks for working on adding filter support to aggregate. I think adding
backends that don't support it using CASE makes a lot of sense[0].
As I've mentioned on your PR this is syntactic sugar I've been missing from
Django's conditional aggregation API since I was using django-aggregate-if[1]
before #11305 was fixed[2].
Now for the proposed syntax I understand you want to mimic the Queryset's
API by allowing filter()/exclude() to be chained but I don't think filter chaining
on aggregate is common enough to warrant the extra effort required to make it
work. As I've mentioned previously I'd advocate for a simple `filter` kwarg that
accepts a Q instance instead as it makes it easier to implement and emulate
the Case() fallback. This also makes the filter() vs filter().filter() for multi-valued
relationships subtleties a non-issue.
Mailbox.objects.annotate(
read_emails=Count('emails', filter=Q(unread=False)),
unread_emails=Count('emails', filter=Q(unread=True)),
recent_emails=Count('emails', filter=Q(received_date__lt=one_week_ago)),
)
Cheers,
Simon