djangodocs.org

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

QuerySet API reference

Reference

This document describes the details of the QuerySet API. It builds on the material presented in the model and database query guides, so you’ll probably want to read and understand those documents before reading this one.

dev

Reference QuerySet API reference

Internally, a QuerySet can be constructed, filtered, sliced, and generally passed around without actually hitting the database. No database activity actually occurs until you do something to evaluate the queryset. You can evaluate a QuerySet in the following ways: Iteration.

dev

QuerySet API

Reference QuerySet API reference

Here’s the formal declaration of a QuerySet: classQuerySet(model=None,query=None,using=None,hints=None)[source] : Usually when you’ll interact with a QuerySet you’ll use it by chaining filters. To make this work, most QuerySet methods return new querysets.

dev

Reference QuerySet API reference

The following QuerySet methods evaluate the QuerySet and return something other than a QuerySet. These methods do not use a cache (see Caching and QuerySets). Rather, they query the database each time they’re called.

dev

Ordered queryset

Reference QuerySet API reference

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.

dev

QuerySets are lazy

Topic guide Making queries

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.

dev

Topic guide Making 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.

dev

QuerySet API reference

Reference

This document describes the details of the QuerySet API. It builds on the material presented in the model and database query guides, so you’ll probably want to read and understand those documents before reading this one.

4.2 5.0 5.1 5.2 6.0

Reference QuerySet API reference

Internally, a QuerySet can be constructed, filtered, sliced, and generally passed around without actually hitting the database. No database activity actually occurs until you do something to evaluate the queryset. You can evaluate a QuerySet in the following ways: Iteration.

4.2 5.0 5.1 5.2 6.0

QuerySet API

Reference QuerySet API reference

Here’s the formal declaration of a QuerySet: classQuerySet(model=None,query=None,using=None,hints=None)[source] : Usually when you’ll interact with a QuerySet you’ll use it by chaining filters. To make this work, most QuerySet methods return new querysets.

4.2 5.0 5.1 5.2 6.0