djangodocs.org

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

Topic guide Aggregation

Fields that are mentioned in the order_by() part of a queryset are used when selecting the output data, even if they are not otherwise specified in the values() call.

dev

Topic guide Making queries

Keyword argument queries – in filter(), etc. – are “AND”ed together. If you need to execute more complex queries (for example, queries with OR statements), you can use Q() objects.

dev

Topic guide Aggregation Generating aggregates for each item in a QuerySet

… most aggregates, there is no way to avoid this problem, however, the Count aggregate has a distinct parameter that may help: NOTE: In order to understand what happens in your query, consider inspecting the > query property of your QuerySet.

4.2 5.0 5.1 5.2 6.0

ModelMultipleChoiceField

Reference Form fields Fields which handle relationships

classModelMultipleChoiceField(**kwargs)[source] : - Default widget: SelectMultiple - Empty value: An empty QuerySet (self.queryset.none()) - Normalizes to: A QuerySet of model instances. - Validates that every id in the given list of values exists in the queryset.

4.2 5.0 5.1 5.2 6.0

Q

Index

… - quarter - field lookup type - query_pk_and_slug (django.views.generic.detail.SingleObjectMixin attribute) - query_string (django.views.generic.base.RedirectView attribute) - QueryDict (class in django.http) - queryset | - QuerySet (class in django.db.models.query) - queryset (django.views.generic.detail.SingleObjectMixin attribute) - (django.views.generic.list.MultipleObjectMixin attribute) - (ModelChoiceField attribute) - (ModelMultipleChoiceField attribute) …

2028

Internals Django Deprecation Timeline

Calling QuerySet.values_list() with flat=True and no field name will raise TypeError. The use of None when querying a JSONField top-level value will compile to SQL IS NULL. Use JSONNull to query for a JSON null value instead.

dev

Reference Settings

Default: False Set this to True if you want to disable the use of server-side cursors with QuerySet.iterator(). Transaction pooling and server-side cursors describes the use case. This is a PostgreSQL-specific setting.

dev

values()

Topic guide Aggregation

Ordinarily, annotations are generated on a per-object basis - an annotated QuerySet will return one result for each object in the original QuerySet.

dev

Topic guide

Article.objects.filter(publications__title__startswith="Science").distinct() <QuerySet [<Article: NASA uses Python>]> Reverse m2m queries are supported (i.e., starting at the table that doesn’t have a ManyToManyField): Excluding a related item works as you would expect, too (although the SQL involved is a little complex): If …

dev

Use iterator()

Topic guide Database access optimization

When you have a lot of objects, the caching behavior of the QuerySet can cause a large amount of memory to be used. In this case, iterator() may help.

dev

Retrieving objects

Topic guide Making queries

To retrieve objects from your database, construct a QuerySet via a Manager on your model class. A QuerySet represents a collection of objects from your database. It can have zero, one or many filters.

dev

Topic guide Making queries

Sometimes you want to set a field to a particular value for all the objects in a QuerySet. You can do this with the update() method. For example: You can only set non-relation fields and ForeignKey fields using this method.

dev

Reference Settings Core SettingsDATABASES

Default: False Set this to True if you want to disable the use of server-side cursors with QuerySet.iterator(). Transaction pooling and server-side cursors describes the use case. This is a PostgreSQL-specific setting.

4.2 5.0 5.1 5.2 6.0

Topic guide

Article.objects.filter(publications__title__startswith="Science").distinct() <QuerySet [<Article: NASA uses Python>]> Reverse m2m queries are supported (i.e., starting at the table that doesn’t have a ManyToManyField): Excluding a related item works as you would expect, too (although the SQL involved is a little complex): If …

4.2 5.0 5.1 5.2 6.0