Model index reference¶
Index classes ease creating database indexes. They can be added using the
Meta.indexes option. This document
explains the API references of
Index which includes the index
Referencing built-in indexes
Indexes are defined in
django.db.models.indexes, but for convenience
they’re imported into
django.db.models. The standard convention is
from django.db import models and refer to the indexes as
Index(fields=(), name=None, db_tablespace=None, opclasses=(), condition=None)¶
Creates an index (B-Tree) in the database.
A list or tuple of the name of the fields on which the index is desired.
By default, indexes are created with an ascending order for each column. To define an index with a descending order for a column, add a hyphen before the field’s name.
Index(fields=['headline', '-pub_date']) would create SQL with
(headline, pub_date DESC). Index ordering isn’t supported on MySQL. In that
case, a descending index is created as a normal index.
The name of the index. If
name isn’t provided Django will auto-generate a
name. For compatibility with different databases, index names cannot be longer
than 30 characters and shouldn’t start with a number (0-9) or underscore (_).
Partial indexes in abstract base classes
You must always specify a unique name for an index. As such, you
cannot normally specify a partial index on an abstract base class, since
Meta.indexes option is
inherited by subclasses, with exactly the same values for the attributes
name) each time. To work around name collisions, part of the
name may contain
'%(class)s', which are
replaced, respectively, by the lowercased app label and class name of the
concrete model. For example
'%(class)s' was added.
The name of the database tablespace to use for
this index. For single field indexes, if
db_tablespace isn’t provided, the
index is created in the
db_tablespace of the field.
Field.db_tablespace isn’t specified (or if the index uses multiple
fields), the index is created in tablespace specified in the
db_tablespace option inside the model’s
class Meta. If neither of those tablespaces are set, the index is created
in the same tablespace as the table.
For a list of PostgreSQL-specific indexes, see
The names of the PostgreSQL operator classes to use for this index. If you require a custom operator class, you must provide one for each field in the index.
opclasses=['jsonb_path_ops']) creates a gin index on
opclasses are ignored for databases besides PostgreSQL.
Index.name is required when using
If the table is very large and your queries mostly target a subset of rows,
it may be useful to restrict an index to that subset. Specify a condition as a
Q. For example,
indexes records with more than 400 pages.
Index.name is required when using
Restrictions on PostgreSQL
PostgreSQL requires functions referenced in the condition to be marked as
IMMUTABLE. Django doesn’t validate this but PostgreSQL will error. This
means that functions such as Date functions and
Concat aren’t accepted. If you store
DateTimeField, comparison to
datetime objects may require the
to be provided because otherwise the comparison could result in a mutable
function due to the casting Django does for lookups.
Restrictions on SQLite
SQLite imposes restrictions on how a partial index can be constructed.
Oracle does not support partial indexes. Instead, partial indexes can be
emulated using functional indexes. Use a migration to add the index using
MySQL and MariaDB
condition argument is ignored with MySQL and MariaDB as neither
supports conditional indexes.