djangodocs.org

241 sections in Django 5.0 Search all versions →

Storage backends

Reference The messages framework Configuring the message engine

The messages framework can use different backends to store temporary messages. Django provides three built-in storage classes in django.contrib.messages: classstorage.session.SessionStorage : This class stores all messages inside of the request’s session. Therefore it requires Django’s contrib.sessions application.

Reference Databases Oracle notes

By default, the Oracle backend uses a RETURNING INTO clause to efficiently retrieve the value of an AutoField when inserting new rows.

Custom colors

Reference django-admin and manage.py Extra nicetiesSyntax coloring

The colors used for syntax highlighting can be customized. Django ships with three color palettes: dark, suited to terminals that show white text on a black background. This is the default palette.

Reference Form fields

Two fields are available for representing relationships between models: ModelChoiceField and ModelMultipleChoiceField. Both of these fields require a single queryset parameter that is used to create the choices for the field.

Reference Form fields Fields which handle relationships

By default, ModelChoiceField and ModelMultipleChoiceField use ModelChoiceIterator to generate their field choices. When iterated, ModelChoiceIterator yields 2-tuple choices containing ModelChoiceIteratorValue instances as the first value element in each choice.

MultiWidget

Reference Widgets Base widget classes

def decompress(self, value): if isinstance(value, date): return [value.day, value.month, value.year] elif isinstance(value, str): year, month, day = value.split("-") return [day, month, year] return [None, None, None] def value_from_datadict(self, data, files, name): day, month, year = super().value_from_datadict(data, files, name) # DateField …

Reference Widgets Built-in widgets

These widgets make use of the HTML elements <select>, <input type="checkbox">, and <input type="radio">. Widgets that render multiple choices have an option_template_name attribute that specifies the template used to render each choice.

Func() expressions

Reference Query Expressions Built-in Expressions

Func() expressions are the base type of all expressions that involve database functions like COALESCE and LOWER, or aggregates like SUM.

Subquery() expressions

Reference Query Expressions Built-in Expressions

classSubquery(queryset,output_field=None) You can add an explicit subquery to a QuerySet using the Subquery expression.

Exists() subqueries

Reference Query Expressions Built-in ExpressionsSubquery() expressions

classExists(queryset) Exists is a Subquery subclass that uses an SQL EXISTS statement. In many cases it will perform better than a subquery since the database is able to stop evaluation of the subquery when a first matching row is found.

Reference Query Expressions Built-in ExpressionsSubquery() expressions

Subquery() that 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 …

exclude()

Reference QuerySet API reference QuerySet APIMethods that return new QuerySets

exclude(*args,**kwargs) Returns a new QuerySet containing objects that do not match the given lookup parameters. The lookup parameters (**kwargs) should be in the format described in Field lookups below.

union()

Reference QuerySet API reference QuerySet APIMethods that return new QuerySets

union(*other_qs,all=False) Uses SQL’s UNION operator to combine the results of two or more QuerySets. For example: The UNION operator selects only distinct values by default. To allow duplicate values, use the all=True argument.

latest()

Reference QuerySet API reference QuerySet APIMethods that do not return QuerySets

latest(*fields) alatest(*fields) Asynchronous version: alatest() Returns the latest object in the table based on the given field(s).

in

Reference QuerySet API reference QuerySet APIField lookups

In a given iterable; often a list, tuple, or queryset. It’s not a common use case, but strings (being iterables) are accepted.

range

Reference QuerySet API reference QuerySet APIField lookups

Range test (inclusive). Example: SQL equivalent: You can use range anywhere you can use BETWEEN in SQL — for dates, numbers and even characters.

regex

Reference QuerySet API reference QuerySet APIField lookups

Case-sensitive regular expression match. The regular expression syntax is that of the database backend in use.

Configuring an engine

Reference The Django template language: for Python programmers

staticEngine.get_default() : Returns the underlying Engine from the first configured DjangoTemplates engine. Raises ImproperlyConfigured if no engines are configured. Engine.from_string(template_code) : Compiles the given template code and returns a Template object.

include

Reference Built-in template tags and filters Built-in tag reference

Loads a template and renders it with the current context. This is a way of “including” other templates within a template. The template name can either be a variable or a hard-coded (quoted) string, in either single or double quotes.