From ee23e03637aa8b82311f93b0a660574a0512891a Mon Sep 17 00:00:00 2001 From: Collin Anderson Date: Tue, 20 Jan 2015 10:20:02 -0500 Subject: [PATCH] Fixed #24190 -- Clarified len(queryset) --- docs/ref/models/querysets.txt | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/docs/ref/models/querysets.txt b/docs/ref/models/querysets.txt index e7d293fc89a..6dca128cbde 100644 --- a/docs/ref/models/querysets.txt +++ b/docs/ref/models/querysets.txt @@ -57,11 +57,10 @@ You can evaluate a ``QuerySet`` in the following ways: * **len().** A ``QuerySet`` is evaluated when you call ``len()`` on it. This, as you might expect, returns the length of the result list. - Note: *Don't* use ``len()`` on ``QuerySet``\s if all you want to do is - determine the number of records in the set. It's much more efficient to - handle a count at the database level, using SQL's ``SELECT COUNT(*)``, - and Django provides a ``count()`` method for precisely this reason. See - ``count()`` below. + Note: If you only need to determine the number of records in the set (and + don't need the actual objects), it's much more efficient to handle a count + at the database level using SQL's ``SELECT COUNT(*)``. Django provides a + :meth:`~QuerySet.count` method for precisely this reason. * **list().** Force evaluation of a ``QuerySet`` by calling ``list()`` on it. For example:: @@ -76,9 +75,8 @@ You can evaluate a ``QuerySet`` in the following ways: if Entry.objects.filter(headline="Test"): print("There is at least one Entry with the headline Test") - Note: *Don't* use this if all you want to do is determine if at least one - result exists, and don't need the actual objects. It's more efficient to - use :meth:`~QuerySet.exists` (see below). + Note: If you only want to determine if at least one result exists (and don't + need the actual objects), it's more efficient to use :meth:`~QuerySet.exists`. .. _pickling QuerySets: @@ -1805,6 +1803,11 @@ Depending on which database you're using (e.g. PostgreSQL vs. MySQL), is an underlying implementation quirk that shouldn't pose any real-world problems. +Note that if you want the number of items in a ``QuerySet`` and are also +retrieving model instances from it (for example, by iterating over it), it's +probably more efficient to use ``len(queryset)`` which won't cause an extra +database query like ``count()`` would. + in_bulk ~~~~~~~