#17311: bool(queryset) calls __len__ instead of __nonzero__ -------------------------------------+------------------------------------- Reporter: adamnelson | Owner: nobody Type: Uncategorized | Status: closed Component: Database layer | Version: (models, ORM) | Resolution: invalid Severity: Normal | Triage Stage: Keywords: | Unreviewed Has patch: 0 | Needs documentation: 0 Needs tests: 0 | Patch needs improvement: 0 Easy pickings: 0 | UI/UX: 0 -------------------------------------+------------------------------------- Changes (by lukeplant):
* status: new => closed * needs_better_patch: => 0 * resolution: => invalid * needs_tests: => 0 * needs_docs: => 0 Comment: Almost everything about this bug report is incorrect, I'm afraid :-) . When running `if queryset`, `__nonzero__` is called, not `__len__` (this is Python behaviour over which we have no control. It is true that `__len__` is called indirectly in some situations in trunk, but not always). The special attribute `__len__` does not call `count()`. Finally `__nonzero__` already exists and does not and should not call `exists()`. The current behaviour is exactly as intended. The reason for this is to make the behaviour 1) predictable, and 2) behave nicely with several common usage patterns, such as: {{{ #!python if queryset: for obj in queryset }}} In short, the rule is you only get special queries (like `exists()` and `count()`) if you ask for them, because we cannot predict if you are going to want to use the `QuerySet`'s result cache or not. All other constructs like `bool()` and `len()` and `iter()` evaluate the same query, and populate/use the same result cache where possible. -- Ticket URL: <https://code.djangoproject.com/ticket/17311#comment:1> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To post to this group, send email to django-updates@googlegroups.com. To unsubscribe from this group, send email to django-updates+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-updates?hl=en.