Opened 17 years ago
Closed 17 years ago
#6899 closed (fixed)
_QuerySet.__nonzero__ returns True for empty query sets with cached results
Reported by: | Brodie Rao | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | queryset-refactor |
Severity: | Keywords: | qs-rf | |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Given any sort of model with zero instances/rows, the following will currently occur:
>>> objects = Foo.objects.all() >>> bool(objects) False >>> bool(objects) True
_QuerySet.__nonzero__
unconditionally returns True
when the result cache is populated, even if nothing is in the cache (i.e. there are no results).
I'm attaching a patch that fixes this behavior, and a test case for it.
Attachments (2)
Change History (7)
by , 17 years ago
Attachment: | nonzero-queryset.diff added |
---|
comment:1 by , 17 years ago
Keywords: | qs-rf added; qs-ref removed |
---|
comment:2 by , 17 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:3 by , 17 years ago
Patch needs improvement: | set |
---|
While this patch fixes the nonzero issue, it appears to break stuff as well. More specifically, while running this patch, django attempted to insert several already existing entries into the database for some reason (with the same primary key) - resulting in several IntegrityErrors. Removing the patch immediately fixed the issue.
by , 17 years ago
Attachment: | nonzero-queryset-2.diff added |
---|
Fixes _QuerySet.nonzero for empty query sets (with test case). Works with ValuesQuerySets.
comment:4 by , 17 years ago
Patch needs improvement: | unset |
---|
I have attached a patch that fixes the issues I had. The first patch relied on QuerySet._result_cache evaluating to True in a boolean context after iter(instance).next() was called. This was not the case with ValuesQuerySets. In this patch, nonzero returns True if the next() call does not raise StopIteration.
comment:5 by , 17 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
Fixes _QuerySet.nonzero for empty query sets (with test case)