djangodocs.org

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

Topic guide Aggregation

… 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.

dev

Topic guide Aggregation

… (note how we use 'book' to specify the Publisher -> Book reverse foreign key hop): (Every Publisher in the resulting QuerySet will have an extra attribute called book__count.) We can also ask for the oldest book of any of those …

dev

Filtering on annotations

Topic guide Aggregation

It’s more efficient to use QuerySet.filter() to exclude > rows. The aggregation filter argument is only useful when using two or > more aggregations over the same relations with different conditionals.

dev

Fetch modes

Topic guide

… can customize the behavior of this fetching with a fetch mode, making it more efficient or even blocking it. Use QuerySet.fetch_mode() to set the fetch mode for model instances fetched by a QuerySet: Fetch modes apply to: ForeignKey fields OneToOneField …

dev

Available modes

Topic guide Fetch modes

We’ll explain them below using these models: …and this loop: …where books is a QuerySet of Book instances using some fetch mode. FETCH_ONE Fetches the missing field for the current instance only. This is the default mode.

dev

Topic guide Managers

… Manager methods.) For example, this custom Manager adds a method with_counts(): With this example, you’d use OpinionPoll.objects.with_counts() to get a QuerySet of OpinionPoll objects with the extra num_responses attribute attached. A custom Manager method can return anything you want. It …

dev

Default managers

Topic guide Managers

As a result, it’s a good idea to be careful in your choice of default manager in order to avoid a situation where overriding get_queryset() results in an inability to retrieve objects you’d like to work with.

dev

Topic guide Managers

For example, if the Question model from the tutorial had a deleted field and a base manager that filters out instances with deleted=True, a queryset like Choice.objects.filter(question__name__startswith='What') would include choices related to deleted questions.

dev

Topic guide Multiple databases

For example, say you have a custom manager method that touches the database – User.objects.create_user(). Because create_user() is a manager method, not a QuerySet method, you can’t do User.objects.using('new_users').create_user().

dev

Insert in bulk

Topic guide Database access optimization

When inserting objects into ManyToManyFields, use add() with multiple objects to reduce the number of SQL queries. For example: …is preferable to: …where Band and Artist are models with a many-to-many relationship.

dev

Remove in bulk

Topic guide Database access optimization

For example: …is preferable to: …where Band and Artist are models with a many-to-many relationship. When removing multiple many-to-many relationships involving several instances of the related models, use the delete() method on a filtered queryset of the field’s through model.

dev

Query iteration

Topic guide Making queries

… you can swap to async for: Be aware that you also can’t do other things that might iterate over the queryset, such as wrapping list() around it to force its evaluation (you can use async for in a comprehension, if …

dev