Fixed #23423 -- Added unaccent lookup in django.contrib.postgres

This commit is contained in:
Thomas Chaumeny 2014-09-05 22:53:11 +02:00 committed by Tim Graham
parent 47789410db
commit 17fe0bd808
12 changed files with 163 additions and 4 deletions

View file

@ -26,5 +26,6 @@ a number of PostgreSQL specific data types.
fields
forms
lookups
operations
validators

View file

@ -0,0 +1,36 @@
===========================
PostgreSQL specific lookups
===========================
Unaccent
========
.. 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).
.. _unaccent extension on PostgreSQL: http://www.postgresql.org/docs/current/interactive/unaccent.html
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.

View file

@ -25,3 +25,11 @@ HStoreExtension
A subclass of :class:`~django.contrib.postgres.operations.CreateExtension`
which will install the ``hstore`` extension and also immediately set up the
connection to interpret hstore data.
UnaccentExtension
-----------------
.. class:: UnaccentExtension()
A subclass of :class:`~django.contrib.postgres.operations.CreateExtension`
which will install the ``unaccent`` extension.