Opened 13 years ago
Closed 13 years ago
#17311 closed Uncategorized (invalid)
bool(queryset) calls __len__ instead of __nonzero__
Reported by: | Adam Nelson | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
When testing the boolean value of a queryset (i.e. running "if queryset: ..."), the special attribute len is called which calls count() in the ORM. It should use the special attribute nonzero which would call the exists() method in the ORM.
Note:
See TracTickets
for help on using tickets.
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 callcount()
. Finally__nonzero__
already exists and does not and should not callexists()
.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:
In short, the rule is you only get special queries (like
exists()
andcount()
) if you ask for them, because we cannot predict if you are going to want to use theQuerySet
's result cache or not. All other constructs likebool()
andlen()
anditer()
evaluate the same query, and populate/use the same result cache where possible.