count()
Returns an integer representing the number of objects in the database matching the QuerySet
. The count()
method never raises exceptions.
Example:
# Returns the total number of entries in the database. Entry.objects.count() # Returns the number of entries whose headline contains 'Lennon' Entry.objects.filter(headline__contains='Lennon').count()
A count()
call performs a SELECT COUNT(*)
behind the scenes, so you should always use count()
rather than loading all of the record into Python objects and calling len()
on the result (unless you need to load the objects into memory anyway, in which case len()
will be faster).
Depending on which database you’re using (e.g. PostgreSQL vs. MySQL), count()
may return a long integer instead of a normal Python integer. This 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.
Please login to continue.