aiven
aiven

Reputation: 4313

How to compare counts in django ORM?

I have model like this:

class A(models.Model):
  done = models.Boolean()

And want to check if all done are True:

A.objects.count() == A.objects.filter(done=True).count()

But how to do the same thing inside DB in one query?

???
A.objects.annotate(Count('done??')).aggregate(??)
???

Upvotes: 1

Views: 148

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599610

You should filter on done=False and check if there are any results with exists.

 A.objects.filter(done=False).exists()

Upvotes: 4

Related Questions