[Answered ]-Django: QuerySet.update() returns FieldError when using Count() with filter

1👍

I solved this by doing the following:

customers = Customer.objects.annotate(
    auc=Count("users", filter=Q(users__is_active=True))
).values("pk", "auc")

customers = [Customer(pk=c["pk"], active_user_count=c["auc"]) for c in customers]

Customer.objects.bulk_update(customers, ["active_user_count"])

Still not sure if it’s the best answer, but it solves the issue in an efficient manner for a total of 2 queries.

👤Ren

Leave a comment