{"title":"PostgreSQL specific lookups","version":"2.0","locale":"id","docname":"ref/contrib/postgres/lookups","url":"/id/2.0/ref/contrib/postgres/lookups/","canonical":"https://djangodocs.dev/id/2.0/ref/contrib/postgres/lookups/","summary":"Trigram similarity Link to this heading # The trigram_similar lookup allows you to perform trigram lookups, measuring the number of trigrams (three consecutive…","html":"<h1>PostgreSQL specific lookups<a class=\"heading-anchor\" href=\"#postgresql-specific-lookups\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<section id=\"trigram-similarity\">\n<h2>Trigram similarity<a class=\"heading-anchor\" href=\"#trigram-similarity\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p id=\"std-fieldlookup-trigram_similar\">The <code class=\"docutils literal notranslate\"><span class=\"pre\">trigram_similar</span></code> lookup allows you to perform trigram lookups,\nmeasuring the number of trigrams (three consecutive characters) shared, using a\ndedicated PostgreSQL extension. A trigram lookup is given an expression and\nreturns results that have a similarity measurement greater than the current\nsimilarity threshold.</p>\n<p>To use it, add <code class=\"docutils literal notranslate\"><span class=\"pre\">'django.contrib.postgres'</span></code> in your <a class=\"reference internal\" href=\"/id/2.0/ref/settings/#std-setting-INSTALLED_APPS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">INSTALLED_APPS</span></code></a>\nand activate the <a class=\"reference external\" href=\"https://www.postgresql.org/docs/current/static/pgtrgm.html\">pg_trgm extension</a> on\nPostgreSQL. You can install the extension using the\n<a class=\"reference internal\" href=\"/id/2.0/ref/contrib/postgres/operations/#django.contrib.postgres.operations.TrigramExtension\" title=\"django.contrib.postgres.operations.TrigramExtension\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">TrigramExtension</span></code></a> migration\noperation.</p>\n<p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">trigram_similar</span></code> lookup can be used on\n<a class=\"reference internal\" href=\"/id/2.0/ref/models/fields/#django.db.models.CharField\" title=\"django.db.models.CharField\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">CharField</span></code></a> and <a class=\"reference internal\" href=\"/id/2.0/ref/models/fields/#django.db.models.TextField\" title=\"django.db.models.TextField\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">TextField</span></code></a>:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">City</span><span class=\"o\">.</span><span class=\"n\">objects</span><span class=\"o\">.</span><span class=\"n\">filter</span><span class=\"p\">(</span><span class=\"n\">name__trigram_similar</span><span class=\"o\">=</span><span class=\"s2\">&quot;Middlesborough&quot;</span><span class=\"p\">)</span>\n<span class=\"go\">[&#39;&lt;City: Middlesbrough&gt;&#39;]</span>\n</code></pre></div>\n</section>\n<section id=\"unaccent\">\n<h2><code class=\"docutils literal notranslate\"><span class=\"pre\">Unaccent</span></code><a class=\"heading-anchor\" href=\"#unaccent\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p id=\"std-fieldlookup-unaccent\">The <code class=\"docutils literal notranslate\"><span class=\"pre\">unaccent</span></code> lookup allows you to perform accent-insensitive lookups using\na dedicated PostgreSQL extension.</p>\n<p>This lookup is implemented using <a class=\"reference internal\" href=\"/id/2.0/ref/models/lookups/#django.db.models.Transform\" title=\"django.db.models.Transform\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Transform</span></code></a>, so it\ncan be chained with other lookup functions. To use it, you need to add\n<code class=\"docutils literal notranslate\"><span class=\"pre\">'django.contrib.postgres'</span></code> in your <a class=\"reference internal\" href=\"/id/2.0/ref/settings/#std-setting-INSTALLED_APPS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">INSTALLED_APPS</span></code></a> and activate\nthe <a class=\"reference external\" href=\"https://www.postgresql.org/docs/current/static/unaccent.html\">unaccent extension on PostgreSQL</a>. The\n<a class=\"reference internal\" href=\"/id/2.0/ref/contrib/postgres/operations/#django.contrib.postgres.operations.UnaccentExtension\" title=\"django.contrib.postgres.operations.UnaccentExtension\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">UnaccentExtension</span></code></a> migration\noperation is available if you want to perform this activation using migrations).</p>\n<p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">unaccent</span></code> lookup can be used on\n<a class=\"reference internal\" href=\"/id/2.0/ref/models/fields/#django.db.models.CharField\" title=\"django.db.models.CharField\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">CharField</span></code></a> and <a class=\"reference internal\" href=\"/id/2.0/ref/models/fields/#django.db.models.TextField\" title=\"django.db.models.TextField\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">TextField</span></code></a>:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">City</span><span class=\"o\">.</span><span class=\"n\">objects</span><span class=\"o\">.</span><span class=\"n\">filter</span><span class=\"p\">(</span><span class=\"n\">name__unaccent</span><span class=\"o\">=</span><span class=\"s2\">&quot;México&quot;</span><span class=\"p\">)</span>\n<span class=\"go\">[&#39;&lt;City: Mexico&gt;&#39;]</span>\n\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">User</span><span class=\"o\">.</span><span class=\"n\">objects</span><span class=\"o\">.</span><span class=\"n\">filter</span><span class=\"p\">(</span><span class=\"n\">first_name__unaccent__startswith</span><span class=\"o\">=</span><span class=\"s2\">&quot;Jerem&quot;</span><span class=\"p\">)</span>\n<span class=\"go\">[&#39;&lt;User: Jeremy&gt;&#39;, &#39;&lt;User: Jérémy&gt;&#39;, &#39;&lt;User: Jérémie&gt;&#39;, &#39;&lt;User: Jeremie&gt;&#39;]</span>\n</code></pre></div>\n<aside class=\"admonition admonition-warning\" role=\"note\">\n<p class=\"admonition-title\">Peringatan</p>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">unaccent</span></code> lookups should perform fine in most use cases. However, queries\nusing this filter will generally perform full table scans, which can be slow\non large tables. In those cases, using dedicated full text indexing tools\nmight be appropriate.</p>\n</aside>\n</section>","rootId":"postgresql-specific-lookups","toc":[{"title":"Trigram similarity","anchor":"trigram-similarity","children":[]},{"title":"Unaccent","anchor":"unaccent","children":[]}],"breadcrumbs":[{"docname":"ref/index","title":"Acuan API","url":"/id/2.0/ref/"},{"docname":"ref/contrib/index","title":"Paket contrib","url":"/id/2.0/ref/contrib/"},{"docname":"ref/contrib/postgres/index","title":"django.contrib.postgres","url":"/id/2.0/ref/contrib/postgres/"}],"prev":{"docname":"ref/contrib/postgres/indexes","title":"PostgreSQL specific model indexes","url":"/id/2.0/ref/contrib/postgres/indexes/"},"next":{"docname":"ref/contrib/postgres/operations","title":"Database migration operations","url":"/id/2.0/ref/contrib/postgres/operations/"},"formats":{"html":"/id/2.0/ref/contrib/postgres/lookups/","markdown":"/id/2.0/ref/contrib/postgres/lookups.md","json":"/id/2.0/ref/contrib/postgres/lookups.json"},"source":"https://github.com/django/django/blob/stable/2.0.x/docs/ref/contrib/postgres/lookups.txt","official":"https://docs.djangoproject.com/id/2.0/ref/contrib/postgres/lookups/","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","1.10","1.9"],"inLocales":["en","zh-hans","fr","ja","id","pt-br","ko","es","el","pl"]}