djangodocs.org

728 sections in Django 5.2 Search all versions →

Topic guide Making queries Related objectsOne-to-many relationships

In addition to the QuerySet methods defined in “Retrieving objects” above, the ForeignKey Manager has additional methods used to handle the set of related objects.

Comparison functions

Reference PostgreSQL specific model fields Range FieldsQuerying Range Fields

Range fields support the standard lookups: lt, gt, lte and gte. These are not particularly helpful - they compare the lower bounds first and then the upper bounds only if necessary.

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.

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.

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

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.

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 …

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.

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.

How-to

Django offers a wide variety of built-in lookups for filtering (for example, exact and icontains). This documentation explains how to write custom lookups and how to alter the working of existing lookups.

A

Index

… | | --- | --- | | - A (class in django.contrib.gis.measure) - aadd() (RelatedManager method) - aaggregate() (in module django.db.models.query.QuerySet) - aauthenticate() (in module django.contrib.auth) - (ModelBackend method) - (RemoteUserBackend method) - Abs (class in django.db.models.functions) - ABSOLUTE_URL_OVERRIDES - …

E

Index

| | | | --- | --- | | - each_context() (AdminSite method) - earliest() (in module django.db.models.query.QuerySet) - editable (Field attribute) - ELLIPSIS (Paginator attribute) - ellipsoid (SpatialReference attribute) - email (models.User attribute) - EMAIL_BACKEND - setting - EMAIL_FIELD …

I

Index

… - import_wkt() (SpatialReference method) - import_xml() (SpatialReference method) - ImproperlyConfigured - in - field lookup type - in_bulk() (in module django.db.models.query.QuerySet) - include - template tag - include (ExclusionConstraint attribute) - (Index attribute) - (UniqueConstraint attribute) - include() (in module …

SQL efficiency

Design philosophies Database API

This is also why the select_related() QuerySet method exists. It’s an optional performance booster for the common case of selecting “every related object.”

ListView

Reference Generic display views

classdjango.views.generic.list.ListView : A page representing a list of objects. classdjango.views.generic.list.BaseListView : A base view for displaying a list of objects.

DateMixin

Reference Date-based mixins

classDateMixin[source] : A mixin class providing common behavior for all date-based views.

SingleObjectMixin

Reference Single object mixins

classdjango.views.generic.detail.SingleObjectMixin : Provides a mechanism for looking up an object associated with the current HTTP request.

Reference Admin actions Advanced action techniques

Actions may limit their availability to users with specific permissions by wrapping the action function with the action() decorator and passing the permissions argument: The make_published() action will only be available to users that pass the ModelAdmin.has_change_permission() check.

Using a SimpleListFilter

Reference ModelAdmin List Filters

… own list filter by subclassing django.contrib.admin.SimpleListFilter. You need to provide the title and parameter_name attributes, and override the lookups and queryset methods, e.g.: NOTE: As a convenience, the HttpRequest object is passed to the lookups > and queryset methods, for …