djangodocs.org

1413 sections across all versions Narrow to Django 6.0 (current) →

Ordered queryset

Reference QuerySet API reference QuerySet APIMethods that do not return QuerySetsupdate()

Chaining order_by() with update() is supported only on MariaDB and MySQL, and is ignored for different databases. This is useful for updating a unique field in the order that is specified without conflicts.

4.2 5.0 5.1 5.2 6.0

QuerySets are lazy

Topic guide Making queries Retrieving objectsRetrieving specific objects with filters

QuerySet objects are lazy – the act of creating a QuerySet doesn’t involve any database activity. You can stack filters together all day long, and Django won’t actually run the query until the QuerySet is evaluated.

4.2 5.0 5.1 5.2 6.0

Topic guide Making queries Asynchronous queries

Some methods on managers and querysets - like get() and first() - force execution of the queryset and are blocking. Some, like filter() and exclude(), don’t force execution and so are safe to run from asynchronous code.

4.2 5.0 5.1 5.2 6.0

from_queryset()

Topic guide Managers

classmethodfrom_queryset(queryset_class) For advanced usage you might want both a custom Manager and a custom QuerySet.

dev

Topic guide Making queries

Each time you refine a QuerySet, you get a brand-new QuerySet that is in no way bound to the previous QuerySet. Each refinement creates a separate and distinct QuerySet that can be stored, used and reused.

dev

Other QuerySet methods

Topic guide Making queries

However, that’s far from all there is; see the QuerySet API Reference for a complete list of all the various QuerySet methods.

dev

Limiting QuerySets

Topic guide Making queries

Use a subset of Python’s array-slicing syntax to limit your QuerySet to a certain number of results. This is the equivalent of SQL’s LIMIT and OFFSET clauses.

dev

Topic guide Making queries

Querysets do not always cache their results. When evaluating only part of the queryset, the cache is checked, but if it is not populated then the items returned by the subsequent query are not cached.

dev