{"title":"Model index reference","version":"2.1","locale":"pt-br","docname":"ref/models/indexes","url":"/pt-br/2.1/ref/models/indexes/","canonical":"https://djangodocs.dev/pt-br/2.1/ref/models/indexes/","summary":"Index classes ease creating database indexes. They can be added using the Meta.indexes option.","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 para este cabeçalho</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>Index classes ease creating database indexes. They can be added using the\n<a class=\"reference internal\" href=\"/pt-br/2.1/ref/models/options/#django.db.models.Options.indexes\" title=\"django.db.models.Options.indexes\"><code class=\"xref py py-attr docutils literal notranslate\">Meta.indexes</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\">Index</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\" role=\"note\">\n<p class=\"admonition-title\">Referencing built-in indexes</p>\n<p>Indexes are defined in <code class=\"docutils literal notranslate\">django.db.models.indexes</code>, but for convenience\nthey’re imported into <a class=\"reference internal\" href=\"/pt-br/2.1/topics/db/models/#module-django.db.models\" title=\"django.db.models\"><code class=\"xref py py-mod docutils literal notranslate\">django.db.models</code></a>. The standard convention is\nto use <code class=\"docutils literal notranslate\">from django.db import models</code> and refer to the indexes as\n<code class=\"docutils literal notranslate\">models.&lt;IndexClass&gt;</code>.</p>\n</aside>\n<section id=\"index-options\">\n<h2><code class=\"docutils literal notranslate\">Index</code> options<a class=\"heading-anchor\" href=\"#index-options\"><span class=\"visually-hidden\">Link para este cabeçalho</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\">class</span> </em><span class=\"sig-name descname\">Index</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">fields<span class=\"o\">=</span><span class=\"default_value\">()</span></em>, <em class=\"sig-param\">name<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">db_tablespace<span class=\"o\">=</span><span class=\"default_value\">None</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.models.Index\"><span class=\"visually-hidden\">Link para esta definição</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\">fields</code><a class=\"heading-anchor\" href=\"#fields\"><span class=\"visually-hidden\">Link para este cabeçalho</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\">Index.</span><span class=\"sig-name descname\">fields</span><a class=\"heading-anchor\" href=\"#django.db.models.Index.fields\"><span class=\"visually-hidden\">Link para esta definição</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>A list or tuple 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\">Index(fields=['headline', '-pub_date'])</code> would create SQL with\n<code class=\"docutils literal notranslate\">(headline, pub_date DESC)</code>. Index ordering isn’t supported on MySQL. In that\ncase, a descending index is created as a normal index.</p>\n<aside class=\"version-note version-changed\" data-version=\"2.1\" role=\"note\">\n<p class=\"version-note-title\">Changed in Django 2.1</p><p>Older versions don’t accept a tuple.</p>\n</aside>\n</section>\n<section id=\"name\">\n<h3><code class=\"docutils literal notranslate\">name</code><a class=\"heading-anchor\" href=\"#name\"><span class=\"visually-hidden\">Link para este cabeçalho</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\">Index.</span><span class=\"sig-name descname\">name</span><a class=\"heading-anchor\" href=\"#django.db.models.Index.name\"><span class=\"visually-hidden\">Link para esta definição</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\">name</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\">db_tablespace</code><a class=\"heading-anchor\" href=\"#db-tablespace\"><span class=\"visually-hidden\">Link para este cabeçalho</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\">Index.</span><span class=\"sig-name descname\">db_tablespace</span><a class=\"heading-anchor\" href=\"#django.db.models.Index.db_tablespace\"><span class=\"visually-hidden\">Link para esta definição</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<aside class=\"version-note version-added\" data-version=\"2.0\" role=\"note\">\n<p class=\"version-note-title\">New in Django 2.0</p></aside>\n<p>The name of the <a class=\"reference internal\" href=\"/pt-br/2.1/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\">db_tablespace</code> isn’t provided, the\nindex is created in the <code class=\"docutils literal notranslate\">db_tablespace</code> of the field.</p>\n<p>If <a class=\"reference internal\" href=\"/pt-br/2.1/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\">Field.db_tablespace</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=\"/pt-br/2.1/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\">db_tablespace</code></a> option inside the model’s\n<code class=\"docutils literal notranslate\">class Meta</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\" role=\"note\">\n<p class=\"admonition-title\">Ver também</p>\n<p>For a list of PostgreSQL-specific indexes, see\n<a class=\"reference internal\" href=\"/pt-br/2.1/ref/contrib/postgres/indexes/#module-django.contrib.postgres.indexes\" title=\"django.contrib.postgres.indexes\"><code class=\"xref py py-mod docutils literal notranslate\">django.contrib.postgres.indexes</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":"Referência da API","url":"/pt-br/2.1/ref/"},{"docname":"ref/models/index","title":"Models","url":"/pt-br/2.1/ref/models/"}],"prev":{"docname":"ref/models/fields","title":"Model field reference","url":"/pt-br/2.1/ref/models/fields/"},"next":{"docname":"ref/models/meta","title":"Model _meta API","url":"/pt-br/2.1/ref/models/meta/"},"formats":{"html":"/pt-br/2.1/ref/models/indexes/","markdown":"/pt-br/2.1/ref/models/indexes.md","json":"/pt-br/2.1/ref/models/indexes.json"},"source":"https://github.com/django/django/blob/stable/2.1.x/docs/ref/models/indexes.txt","official":"https://docs.djangoproject.com/pt-br/2.1/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","1.11"],"inLocales":["en","zh-hans","fr","ja","id","pt-br","ko","es","el","pl"]}