

database statistics

QuerySets are lazy


>>> q = q.filter(
>>> print(q)

Though this looks like three database hits, in fact it hits the database only once, at the last line (print(q)). In general, the results of a QuerySet aren’t fetched from the database until you “ask” for them. When you do, the QuerySet is evaluatedby accessing the database. For more details on exactly when evaluation takes place, see When QuerySets are evaluated.

Retrieving a single object with get()

filter() will always give you a QuerySet, even if only a single object matches the query – in this case, it will be aQuerySet containing a single element.


>>> one_entry = Entry.objects.get(pk=1)

You can use any query expression with get(), just like with filter() – again, see Field lookups below.


Similarly, Django will complain if more than one item matches the get() query. In this case, it will raiseMultipleObjectsReturned, which again is an attribute of the model class itself.

Other QuerySet methods

Most of the time you’ll use all()get()filter() and exclude() when you need to look up objects from the database. However, that’s far from all there is; see the QuerySet API Reference for a complete list of all the various QuerySet methods.