mirror of
				https://github.com/django/django.git
				synced 2025-10-31 09:41:08 +00:00 
			
		
		
		
	Fixed #24190 -- Clarified len(queryset)
This commit is contained in:
		
				
					committed by
					
						 Tim Graham
						Tim Graham
					
				
			
			
				
	
			
			
			
						parent
						
							0386b97706
						
					
				
				
					commit
					ee23e03637
				
			| @@ -57,11 +57,10 @@ You can evaluate a ``QuerySet`` in the following ways: | |||||||
| * **len().** A ``QuerySet`` is evaluated when you call ``len()`` on it. | * **len().** A ``QuerySet`` is evaluated when you call ``len()`` on it. | ||||||
|   This, as you might expect, returns the length of the result list. |   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 |   Note: If you only need to determine the number of records in the set (and | ||||||
|   determine the number of records in the set. It's much more efficient to |   don't need the actual objects), it's much more efficient to handle a count | ||||||
|   handle a count at the database level, using SQL's ``SELECT COUNT(*)``, |   at the database level using SQL's ``SELECT COUNT(*)``. Django provides a | ||||||
|   and Django provides a ``count()`` method for precisely this reason. See |   :meth:`~QuerySet.count` method for precisely this reason. | ||||||
|   ``count()`` below. |  | ||||||
|  |  | ||||||
| * **list().** Force evaluation of a ``QuerySet`` by calling ``list()`` on | * **list().** Force evaluation of a ``QuerySet`` by calling ``list()`` on | ||||||
|   it. For example:: |   it. For example:: | ||||||
| @@ -76,9 +75,8 @@ You can evaluate a ``QuerySet`` in the following ways: | |||||||
|       if Entry.objects.filter(headline="Test"): |       if Entry.objects.filter(headline="Test"): | ||||||
|          print("There is at least one Entry with the 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 |   Note: If you only want to determine if at least one result exists (and don't | ||||||
|   result exists, and don't need the actual objects. It's more efficient to |   need the actual objects), it's more efficient to use :meth:`~QuerySet.exists`. | ||||||
|   use :meth:`~QuerySet.exists` (see below). |  | ||||||
|  |  | ||||||
| .. _pickling QuerySets: | .. _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 | is an underlying implementation quirk that shouldn't pose any real-world | ||||||
| problems. | 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 | in_bulk | ||||||
| ~~~~~~~ | ~~~~~~~ | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user