2014-09-05 20:53:11 +00:00
|
|
|
===========================
|
|
|
|
PostgreSQL specific lookups
|
|
|
|
===========================
|
|
|
|
|
2015-06-05 16:37:48 +00:00
|
|
|
Trigram similarity
|
|
|
|
==================
|
|
|
|
|
|
|
|
.. fieldlookup:: trigram_similar
|
|
|
|
|
|
|
|
The ``trigram_similar`` lookup allows you to perform trigram lookups,
|
|
|
|
measuring the number of trigrams (three consecutive characters) shared, using a
|
|
|
|
dedicated PostgreSQL extension. A trigram lookup is given an expression and
|
|
|
|
returns results that have a similarity measurement greater than the current
|
|
|
|
similarity threshold.
|
|
|
|
|
|
|
|
To use it, add ``'django.contrib.postgres'`` in your :setting:`INSTALLED_APPS`
|
|
|
|
and activate the `pg_trgm extension
|
2016-10-25 15:43:32 +00:00
|
|
|
<https://www.postgresql.org/docs/current/static/pgtrgm.html>`_ on
|
2015-06-05 16:37:48 +00:00
|
|
|
PostgreSQL. You can install the extension using the
|
|
|
|
:class:`~django.contrib.postgres.operations.TrigramExtension` migration
|
|
|
|
operation.
|
|
|
|
|
|
|
|
The ``trigram_similar`` lookup can be used on
|
|
|
|
:class:`~django.db.models.CharField` and :class:`~django.db.models.TextField`::
|
|
|
|
|
|
|
|
>>> City.objects.filter(name__trigram_similar="Middlesborough")
|
|
|
|
['<City: Middlesbrough>']
|
|
|
|
|
2016-01-24 21:26:11 +00:00
|
|
|
``Unaccent``
|
|
|
|
============
|
2014-09-05 20:53:11 +00:00
|
|
|
|
|
|
|
.. fieldlookup:: unaccent
|
|
|
|
|
|
|
|
The ``unaccent`` lookup allows you to perform accent-insensitive lookups using
|
|
|
|
a dedicated PostgreSQL extension.
|
|
|
|
|
|
|
|
This lookup is implemented using :class:`~django.db.models.Transform`, so it
|
|
|
|
can be chained with other lookup functions. To use it, you need to add
|
|
|
|
``'django.contrib.postgres'`` in your :setting:`INSTALLED_APPS` and activate
|
|
|
|
the `unaccent extension on PostgreSQL`_. The
|
|
|
|
:class:`~django.contrib.postgres.operations.UnaccentExtension` migration
|
|
|
|
operation is available if you want to perform this activation using migrations).
|
|
|
|
|
2016-10-25 15:43:32 +00:00
|
|
|
.. _unaccent extension on PostgreSQL: https://www.postgresql.org/docs/current/static/unaccent.html
|
2014-09-05 20:53:11 +00:00
|
|
|
|
|
|
|
The ``unaccent`` lookup can be used on
|
|
|
|
:class:`~django.db.models.CharField` and :class:`~django.db.models.TextField`::
|
|
|
|
|
|
|
|
>>> City.objects.filter(name__unaccent="México")
|
|
|
|
['<City: Mexico>']
|
|
|
|
|
|
|
|
>>> User.objects.filter(first_name__unaccent__startswith="Jerem")
|
|
|
|
['<User: Jeremy>', '<User: Jérémy>', '<User: Jérémie>', '<User: Jeremie>']
|
|
|
|
|
|
|
|
.. warning::
|
|
|
|
|
|
|
|
``unaccent`` lookups should perform fine in most use cases. However, queries
|
|
|
|
using this filter will generally perform full table scans, which can be slow
|
|
|
|
on large tables. In those cases, using dedicated full text indexing tools
|
|
|
|
might be appropriate.
|