I have the following models:
class Foo(models.Model):
pass
class Bar(models.Model):
foo = models.ForeignKey(Foo)
is_successful = models.BooleanField()
I would like to get all objects foowith annotation if all objects barassociated with the object foohave is_successfulasTrue
So far, my request is:
foos = Foo.objects.all().annotate(all_successful=Min('bar__is_successful'))
The idea of annotation all_successfulis that if the minimum value of all lines is_successfulis 1, then they should all be True(it is assumed that 0is Falseand 1is True). Therefore, knowing that I can use a set of queries like this:
foo = foos[0]
if foo.all_successful == 1:
print 'All bars are successful'
else:
print 'Not all bars are successful'
This works fine in sqlite, however it doesn't work in PostgreSQL because PostgreSQL cannot aggregate MINin a boolean column. I think this works in sqlite because sqlite treats bools as integers, so it can execute an aggregate.
, PostgreSQL is_successful IntegerField?
Thanx