Model index referenceLink para este cabeçalho

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

Index optionsLink para este cabeçalho

class Index(*expressions, fields=(), name=None, db_tablespace=None, opclasses=(), condition=None, include=None)Link para esta definição

Creates an index (B-Tree) in the database.

expressionsLink para este cabeçalho

Index.expressionsLink para esta definição

Positional argument *expressions allows creating functional indexes on expressions and database functions.

Por exemplo:

Code
Index(Lower('title').desc(), 'pub_date', name='lower_title_date_idx')

creates an index on the lowercased value of the title field in descending order and the pub_date field in the default ascending order.

Another example:

Code
Index(F('height') * F('weight'), Round('weight'), name='calc_idx')

creates an index on the result of multiplying fields height and weight and the weight rounded to the nearest integer.

Index.name is required when using *expressions.

fieldsLink para este cabeçalho

Index.fieldsLink para esta definição

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.

For example 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.

nameLink para este cabeçalho

Index.nameLink para esta definição

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

db_tablespaceLink para este cabeçalho

Index.db_tablespaceLink para esta definição

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.

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

opclassesLink para este cabeçalho

Index.opclassesLink para esta definição

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.

For example, GinIndex(name='json_index', fields=['jsonfield'], opclasses=['jsonb_path_ops']) creates a gin index on jsonfield using jsonb_path_ops.

opclasses are ignored for databases besides PostgreSQL.

Index.name is required when using opclasses.

conditionLink para este cabeçalho

Index.conditionLink para esta definição

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, condition=Q(pages__gt=400) indexes records with more than 400 pages.

Index.name is required when using condition.

includeLink para este cabeçalho

Index.includeLink para esta definição

A list or tuple of the names of the fields to be included in the covering index as non-key columns. This allows index-only scans to be used for queries that select only included fields (include) and filter only by indexed fields (fields).

Por exemplo:

Code
Index(name='covering_index', fields=['headline'], include=['pub_date'])

will allow filtering on headline, also selecting pub_date, while fetching data only from the index.

Using include will produce a smaller index than using a multiple column index but with the drawback that non-key columns can not be used for sorting or filtering.

include is ignored for databases besides PostgreSQL.

Index.name is required when using include.

See the PostgreSQL documentation for more details about covering indexes.