djangodocs.org

674 sections in Django 4.2 Search all versions →

Reference Query Expressions Built-in ExpressionsSubquery() expressions

… returns a boolean value and Exists() may be used as a condition in When expressions, or to directly filter a queryset: This will ensure that the subquery will not be added to the SELECT columns, which may result in a …

Reference Query Expressions Built-in ExpressionsSubquery() 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.

Raw SQL expressions

Reference Query Expressions Built-in Expressions

classRawSQL(sql,params,output_field=None) Sometimes database expressions can’t easily express a complex WHERE clause. In these edge cases, use the RawSQL expression.

Paginator class

Reference Paginator

Paginator.object_list : Required. A list, tuple, QuerySet, or other sliceable object with a count() or __len__() method. For consistent pagination, QuerySets should be ordered, e.g. with an order_by() clause or with a default ordering on the model. Paginator.per_page : Required.

pre_delete

Reference Signals Model 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 Model 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.

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.

Update in bulk

Topic guide Database access optimization Use bulk methods

Given a list or queryset of objects: The following example: …is preferable to: Note that there are a number of caveats to this method, so make sure it’s appropriate for your use case.

Retrieving all objects

Topic guide Making queries Retrieving objects

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 Retrieving objects

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

Deferring model fields

Topic guide Performing raw SQL queries Performing raw queries

Fields may also be left out: The Person objects returned by this query will be deferred model instances (see defer()). This means that the fields that are omitted from the query will be loaded on demand.

Field types

Topic guide Creating forms from models ModelForm

As you might expect, the ForeignKey and ManyToManyField model field types are special cases: ForeignKey is represented by django.forms.ModelChoiceField, which is a ChoiceField whose choices are a model QuerySet.

Arguments

Topic guide Django shortcut functions get_list_or_404()

klass A Model class, a Manager, or a QuerySet instance from which to get the object. *args Q objects. **kwargs Lookup parameters, which should be in the format accepted by get() and filter().

Arguments

Topic guide Django shortcut functions get_list_or_404()

klass A Model, Manager or QuerySet instance from which to get the list. *args Q objects. **kwargs Lookup parameters, which should be in the format accepted by get() and filter().