Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#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:2 by Tim Graham, 10 years ago

Patch needs improvement: set
Triage Stage: UnreviewedAccepted

comment:3 by Raúl Cumplido, 10 years ago

Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin

comment:4 by Tim Graham <timograham@…>, 10 years ago

Resolution: fixed
Status: newclosed

In ee23e03637aa8b82311f93b0a660574a0512891a:

Fixed #24190 -- Clarified len(queryset)

comment:5 by Tim Graham <timograham@…>, 10 years ago

In 09e8985f25ddd901a3678d4b34878d6c631b0cea:

[1.8.x] Fixed #24190 -- Clarified len(queryset)

Backport of ee23e03637aa8b82311f93b0a660574a0512891a from master

comment:6 by Tim Graham <timograham@…>, 10 years ago

In 6dc6ec218860e4d4c5c1833c9b8607c4014c85d7:

[1.7.x] Fixed #24190 -- Clarified len(queryset)

Backport of ee23e03637aa8b82311f93b0a660574a0512891a from master

Note: See TracTickets for help on using tickets.
Back to Top