#24190 closed Cleanup/optimization (fixed)
"Don’t use len() on QuerySets" is too strong.
Reported by: | Collin Anderson | Owned by: | nobody |
---|---|---|---|
Component: | Documentation | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Ready for checkin | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
I've seen people write code like:
qs = Model.objects.filter(a=b) if qs.count() >= 1: for q in qs: # do something with q
They think the that ".count()" call is more efficient, but it actually makes it slower. I asked the person about it they said: "the docs say don't use len() on querysets".
Change History (6)
comment:1 by , 10 years ago
Has patch: | set |
---|
comment:2 by , 10 years ago
Patch needs improvement: | set |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:3 by , 10 years ago
Patch needs improvement: | unset |
---|---|
Triage Stage: | Accepted → Ready for checkin |
comment:4 by , 10 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
Note:
See TracTickets
for help on using tickets.
https://github.com/django/django/pull/3961