2016-06-26 00:32:56 +08:00
|
|
|
=====================
|
|
|
|
Model index reference
|
|
|
|
=====================
|
|
|
|
|
|
|
|
.. module:: django.db.models.indexes
|
|
|
|
|
|
|
|
.. currentmodule:: django.db.models
|
|
|
|
|
2016-06-20 23:50:05 +08:00
|
|
|
Index classes ease creating database indexes. They can be added using the
|
|
|
|
:attr:`Meta.indexes <django.db.models.Options.indexes>` option. This document
|
|
|
|
explains the API references of :class:`Index` which includes the `index
|
|
|
|
options`_.
|
2016-06-26 00:32:56 +08:00
|
|
|
|
|
|
|
.. admonition:: Referencing built-in indexes
|
|
|
|
|
|
|
|
Indexes are defined in ``django.db.models.indexes``, but for convenience
|
|
|
|
they're imported into :mod:`django.db.models`. The standard convention is
|
|
|
|
to use ``from django.db import models`` and refer to the indexes as
|
|
|
|
``models.<IndexClass>``.
|
|
|
|
|
|
|
|
``Index`` options
|
|
|
|
=================
|
|
|
|
|
2017-06-28 03:15:15 +08:00
|
|
|
.. class:: Index(fields=[], name=None, db_tablespace=None)
|
2016-06-26 00:32:56 +08:00
|
|
|
|
|
|
|
Creates an index (B-Tree) in the database.
|
|
|
|
|
|
|
|
``fields``
|
2017-03-21 06:30:32 +08:00
|
|
|
----------
|
2016-06-26 00:32:56 +08:00
|
|
|
|
|
|
|
.. attribute:: Index.fields
|
|
|
|
|
|
|
|
A list of the name of the fields on which the index is desired.
|
|
|
|
|
2016-07-22 20:52:44 +08:00
|
|
|
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.
|
|
|
|
|
2016-06-26 00:32:56 +08:00
|
|
|
``name``
|
|
|
|
--------
|
|
|
|
|
|
|
|
.. attribute:: Index.name
|
|
|
|
|
|
|
|
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 (_).
|
2016-08-08 19:50:25 +08:00
|
|
|
|
2017-06-28 03:15:15 +08:00
|
|
|
``db_tablespace``
|
|
|
|
-----------------
|
|
|
|
|
|
|
|
.. attribute:: Index.db_tablespace
|
|
|
|
|
|
|
|
.. versionadded:: 2.0
|
|
|
|
|
|
|
|
The name of the :doc:`database tablespace </topics/db/tablespaces>` 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 :attr:`.Field.db_tablespace` isn't specified (or if the index uses multiple
|
|
|
|
fields), the index is created in tablespace specified in the
|
|
|
|
:attr:`~django.db.models.Options.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.
|
|
|
|
|
2016-08-08 19:50:25 +08:00
|
|
|
.. seealso::
|
|
|
|
|
|
|
|
For a list of PostgreSQL-specific indexes, see
|
|
|
|
:mod:`django.contrib.postgres.indexes`.
|