1 min readFeb 2, 2019
Hey Edre,
It’s possible that the implementation vary between db vendors. In PostgreSQL both work as expected:
>>> print( User.objects.annotate(cnt=Count('id', filter=F('is_active'))).values('cnt').query )
SELECT COUNT("auth_user"."id") FILTER (WHERE "auth_user"."is_active") AS "cnt" FROM "auth_user" GROUP BY "auth_user"."id">>> print( User.objects.annotate(cnt=Count('id', filter=Q(is_active=True))).values('cnt').query )
SELECT COUNT("auth_user"."id") FILTER (WHERE "auth_user"."is_active" = True) AS "cnt" FROM "auth_user" GROUP BY "auth_user"."id"
If SQLite is unable to evaluate is_active
you might need to use Q
.