djangodocs.org

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

Topic guide Aggregation Joins and aggregates

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

4.2 5.0 5.1 5.2 6.0

Topic guide Managers Custom 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 …

4.2 5.0 5.1 5.2 6.0

Default managers

Topic guide Managers Custom 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.

4.2 5.0 5.1 5.2 6.0

Topic guide Managers Custom managersBase 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.

4.2 5.0 5.1 5.2 6.0

Topic guide Multiple databases Manually selecting a database

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().

4.2 5.0 5.1 5.2 6.0

Insert in bulk

Topic guide Database access optimization Use bulk methods

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.

4.2 5.0 5.1 5.2 6.0

Remove in bulk

Topic guide Database access optimization Use bulk methods

When removing objects from ManyToManyFields, use remove() 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.

4.2 5.0 5.1 5.2 6.0

Query iteration

Topic guide Making queries Asynchronous 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 …

4.2 5.0 5.1 5.2 6.0

KT() expressions

Topic guide Making queries Querying JSONFieldKey, index, and path transforms

classKT(lookup) : Represents the text value of a key, index, or path transform of JSONField. You can use the double underscore notation in lookup to chain dictionary key and index transforms.

4.2 5.0 5.1 5.2 6.0

Topic guide Making queries Related objectsOne-to-many relationships

This Manager returns QuerySet instances, which can be filtered and manipulated as described in the “Retrieving objects” section above. Example: You can override the FOO_set name by setting the related_name parameter in the ForeignKey definition.

4.2 5.0 5.1 5.2 6.0

Topic guide Making queries Related objectsOne-to-many relationships

If you would like to specify a different manager for a given query you can use the following syntax: If EntryManager performed default filtering in its get_queryset() method, that filtering would apply to the all() call.

4.2 5.0 5.1 5.2 6.0

Topic guide

For example: The QuerySet API is extensive. You can annotate and aggregate using many built-in database functions. Beyond those, you can create > custom query expressions. Before using raw SQL, explore the ORM.

4.2 5.0 5.1 5.2 6.0

Performing raw queries

Topic guide Performing raw SQL queries

The raw() manager method can be used to perform raw SQL queries that return model instances: Manager.raw(raw_query,params=(),translations=None) This method takes a raw SQL query, executes it, and returns a django.db.models.query.RawQuerySet instance.

4.2 5.0 5.1 5.2 6.0