Monday, November 28, 2011

Boolean test on queryset

When one tests for the boolean value of a queryset in the following way:

examples = Example.objects.all()

if examples:
    ..... do something

You might think (I did) that Django will call a __nonzero__ special attribute that would either execute an EXISTS SQL STATEMENT or a SELECT statement with a LIMIT of 1.  Instead, the ORM evaluates the entire queryset through the __len__ attribute.

Is there a best practice for working around this?


You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit
To post to this group, send email to
To unsubscribe from this group, send email to
For more options, visit this group at

No comments:

Post a Comment