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?

Thanks,
Adam

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/G_V4jwWVh4MJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment