{"title":"Model index reference","version":"2.0","locale":"zh-hans","docname":"ref/models/indexes","url":"/zh-hans/2.0/ref/models/indexes/","canonical":"https://djangodocs.dev/zh-hans/2.0/ref/models/indexes/","summary":"New in Django 1.11 Index classes ease creating database indexes. They can be added using the Meta.indexes option. This document explains the API references of Index…","html":"<span id=\"model-index-reference\"></span><h1>Model index reference<a class=\"heading-anchor\" href=\"#module-django.db.models.indexes\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<aside class=\"version-note version-added\" data-version=\"1.11\">\n<p class=\"version-note-title\">New in Django 1.11</p></aside>\n<p>Index classes ease creating database indexes. They can be added using the\n<a class=\"reference internal\" href=\"/zh-hans/2.0/ref/models/options/#django.db.models.Options.indexes\" title=\"django.db.models.Options.indexes\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">Meta.indexes</span></code></a> option. This document\nexplains the API references of <a class=\"reference internal\" href=\"#django.db.models.Index\" title=\"django.db.models.Index\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Index</span></code></a> which includes the <a class=\"reference internal\" href=\"#index-options\">index\noptions</a>.</p>\n<aside class=\"admonition-referencing-built-in-indexes admonition\">\n<p class=\"admonition-title\">Referencing built-in indexes</p>\n<p>Indexes are defined in <code class=\"docutils literal notranslate\"><span class=\"pre\">django.db.models.indexes</span></code>, but for convenience\nthey're imported into <a class=\"reference internal\" href=\"/zh-hans/2.0/topics/db/models/#module-django.db.models\" title=\"django.db.models\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">django.db.models</span></code></a>. The standard convention is\nto use <code class=\"docutils literal notranslate\"><span class=\"pre\">from</span> <span class=\"pre\">django.db</span> <span class=\"pre\">import</span> <span class=\"pre\">models</span></code> and refer to the indexes as\n<code class=\"docutils literal notranslate\"><span class=\"pre\">models.&lt;IndexClass&gt;</span></code>.</p>\n</aside>\n<section id=\"index-options\">\n<h2><code class=\"docutils literal notranslate\"><span class=\"pre\">Index</span></code> options<a class=\"heading-anchor\" href=\"#index-options\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.Index\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">Index</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">fields</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">[]</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">name</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">db_tablespace</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.models.Index\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Creates an index (B-Tree) in the database.</p>\n</dd></dl>\n\n<section id=\"fields\">\n<h3><code class=\"docutils literal notranslate\"><span class=\"pre\">fields</span></code><a class=\"heading-anchor\" href=\"#fields\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.Index.fields\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">Index.</span></span><span class=\"sig-name descname\"><span class=\"pre\">fields</span></span><a class=\"heading-anchor\" href=\"#django.db.models.Index.fields\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>A list of the name of the fields on which the index is desired.</p>\n<p>By default, indexes are created with an ascending order for each column. To\ndefine an index with a descending order for a column, add a hyphen before the\nfield's name.</p>\n<p>For example <code class=\"docutils literal notranslate\"><span class=\"pre\">Index(fields=['headline',</span> <span class=\"pre\">'-pub_date'])</span></code> would create SQL with\n<code class=\"docutils literal notranslate\"><span class=\"pre\">(headline,</span> <span class=\"pre\">pub_date</span> <span class=\"pre\">DESC)</span></code>. Index ordering isn't supported on MySQL. In that\ncase, a descending index is created as a normal index.</p>\n<aside class=\"admonition-support-for-column-ordering-on-sqlite admonition\">\n<p class=\"admonition-title\">Support for column ordering on SQLite</p>\n<p>Column ordering is supported on SQLite 3.3.0+ and only for some database\nfile formats. Refer to the <a class=\"reference external\" href=\"https://www.sqlite.org/lang_createindex.html\">SQLite docs</a> for specifics.</p>\n</aside>\n</section>\n<section id=\"name\">\n<h3><code class=\"docutils literal notranslate\"><span class=\"pre\">name</span></code><a class=\"heading-anchor\" href=\"#name\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.Index.name\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">Index.</span></span><span class=\"sig-name descname\"><span class=\"pre\">name</span></span><a class=\"heading-anchor\" href=\"#django.db.models.Index.name\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>The name of the index. If <code class=\"docutils literal notranslate\"><span class=\"pre\">name</span></code> isn't provided Django will auto-generate a\nname. For compatibility with different databases, index names cannot be longer\nthan 30 characters and shouldn't start with a number (0-9) or underscore (_).</p>\n</section>\n<section id=\"db-tablespace\">\n<h3><code class=\"docutils literal notranslate\"><span class=\"pre\">db_tablespace</span></code><a class=\"heading-anchor\" href=\"#db-tablespace\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.models.Index.db_tablespace\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">Index.</span></span><span class=\"sig-name descname\"><span class=\"pre\">db_tablespace</span></span><a class=\"heading-anchor\" href=\"#django.db.models.Index.db_tablespace\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<aside class=\"version-note version-added\" data-version=\"2.0\">\n<p class=\"version-note-title\">New in Django 2.0</p></aside>\n<p>The name of the <a class=\"reference internal\" href=\"/zh-hans/2.0/topics/db/tablespaces/\"><span class=\"doc\">database tablespace</span></a> to use for\nthis index. For single field indexes, if <code class=\"docutils literal notranslate\"><span class=\"pre\">db_tablespace</span></code> isn't provided, the\nindex is created in the <code class=\"docutils literal notranslate\"><span class=\"pre\">db_tablespace</span></code> of the field.</p>\n<p>If <a class=\"reference internal\" href=\"/zh-hans/2.0/ref/models/fields/#django.db.models.Field.db_tablespace\" title=\"django.db.models.Field.db_tablespace\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">Field.db_tablespace</span></code></a> isn't specified (or if the index uses multiple\nfields), the index is created in tablespace specified in the\n<a class=\"reference internal\" href=\"/zh-hans/2.0/ref/models/options/#django.db.models.Options.db_tablespace\" title=\"django.db.models.Options.db_tablespace\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">db_tablespace</span></code></a> option inside the model's\n<code class=\"docutils literal notranslate\"><span class=\"pre\">class</span> <span class=\"pre\">Meta</span></code>. If neither of those tablespaces are set, the index is created\nin the same tablespace as the table.</p>\n<aside class=\"admonition admonition-seealso\">\n<p class=\"admonition-title\">See also</p>\n<p>For a list of PostgreSQL-specific indexes, see\n<a class=\"reference internal\" href=\"/zh-hans/2.0/ref/contrib/postgres/indexes/#module-django.contrib.postgres.indexes\" title=\"django.contrib.postgres.indexes\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">django.contrib.postgres.indexes</span></code></a>.</p>\n</aside>\n</section>\n</section>","rootId":"module-django.db.models.indexes","toc":[{"title":"Index options","anchor":"index-options","children":[{"title":"fields","anchor":"fields","children":[]},{"title":"name","anchor":"name","children":[]},{"title":"db_tablespace","anchor":"db-tablespace","children":[]}]}],"breadcrumbs":[{"docname":"ref/index","title":"API Reference","url":"/zh-hans/2.0/ref/"},{"docname":"ref/models/index","title":"Models","url":"/zh-hans/2.0/ref/models/"}],"prev":{"docname":"ref/models/fields","title":"Model field reference","url":"/zh-hans/2.0/ref/models/fields/"},"next":{"docname":"ref/models/meta","title":"Model _meta API","url":"/zh-hans/2.0/ref/models/meta/"},"formats":{"html":"/zh-hans/2.0/ref/models/indexes/","markdown":"/zh-hans/2.0/ref/models/indexes.md","json":"/zh-hans/2.0/ref/models/indexes.json"},"source":"https://github.com/django/django/blob/stable/2.0.x/docs/ref/models/indexes.txt","official":"https://docs.djangoproject.com/zh-hans/2.0/ref/models/indexes/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0"],"inLocales":["en","zh-hans","fr","ja","id","pt-br","ko","es","el","pl"]}