djangodocs.org

769 sections in Django dev Search all versions →

Reference Query Expressions

values('post') aggregates comments by Post. Finally, annotate(...) performs the aggregation. The order in which these queryset methods are applied is important. In this case, since the subquery must be limited to a single column, values('total') is required.

Reference Model instance reference

If you delete a field from a model instance, accessing it again reloads the value from the database: Model.refresh_from_db(using=None,fields=None,from_queryset=None)[source] Model.arefresh_from_db(using=None,fields=None,from_queryset=None) Asynchronous version: arefresh_from_db() If you need to reload a model’s values from the database, you can use the refresh_from_db() method.

pre_delete

Reference Signals

django.db.models.signals.pre_delete Sent at the beginning of a model’s delete() method and a queryset’s delete() method. Arguments sent with this signal: sender The model class. instance The actual instance being deleted. using The database alias being used.

post_delete

Reference Signals

django.db.models.signals.post_delete Like pre_delete, but sent at the end of a model’s delete() method and a queryset’s delete() method. Arguments sent with this signal: sender : The model class. instance : The actual instance being deleted.

Queries & the ORM

Topic guide Asynchronous support

With some exceptions, Django can run ORM queries asynchronously: Detailed notes can be found in Asynchronous queries, but in short: All QuerySet methods that cause an SQL query to occur have an a-prefixed asynchronous variant.

order_by()

Topic guide Aggregation

For example, to order a QuerySet of books by the number of authors that have contributed to the book, you could use the following query:

Custom managers

Topic guide Managers

There are two reasons you might want to customize a Manager: to add extra Manager methods, and/or to modify the initial QuerySet the Manager returns.

Profile first

Topic guide Database access optimization

Use QuerySet.explain() to understand how specific QuerySets are executed by your database. You may also want to use an external project like django-debug-toolbar, or a tool that monitors your database directly.

Create in bulk

Topic guide Database access optimization

When creating objects, where possible, use the bulk_create() method to reduce the number of SQL queries. For example: …is preferable to: Note that bulk_create() has several caveats, so ensure it’s appropriate for your use case.

Update in bulk

Topic guide Database access optimization

Given a list or queryset of objects: The following example: …is preferable to: Note that bulk_update() has several caveats, so ensure it’s appropriate for your use case.

Retrieving all objects

Topic guide Making queries

The simplest way to retrieve objects from a table is to get all of them. To do this, use the all() method on a Manager: The all() method returns a QuerySet of all the objects in the database.

Field lookups

Topic guide Making queries

Field lookups are how you specify the meat of an SQL WHERE clause. They’re specified as keyword arguments to the QuerySet methods filter(), exclude() and get(). Basic lookups keyword arguments take the form field__lookuptype=value. (That’s a double-underscore).

No deferred queries

Topic guide Making queries

Relations are deferred by default. If the blog relation is not prefetched, accessing entry.blog during asynchronous evaluation of a queryset attempts a blocking database query, raising SynchronousOnlyOperation.

Raw SQL fragments

Topic guide Performing raw SQL queries

In some cases, you may need to embed raw SQL fragments directly into ORM queries — for example, in annotate() or filter() calls. Use Func() expressions for calling database functions across backends, or RawSQL for arbitrary parameterized SQL fragments.