lookups.txt 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. ===========================
  2. PostgreSQL specific lookups
  3. ===========================
  4. Trigram similarity
  5. ==================
  6. .. fieldlookup:: trigram_similar
  7. .. versionadded:: 1.10
  8. The ``trigram_similar`` lookup allows you to perform trigram lookups,
  9. measuring the number of trigrams (three consecutive characters) shared, using a
  10. dedicated PostgreSQL extension. A trigram lookup is given an expression and
  11. returns results that have a similarity measurement greater than the current
  12. similarity threshold.
  13. To use it, add ``'django.contrib.postgres'`` in your :setting:`INSTALLED_APPS`
  14. and activate the `pg_trgm extension
  15. <https://www.postgresql.org/docs/current/static/pgtrgm.html>`_ on
  16. PostgreSQL. You can install the extension using the
  17. :class:`~django.contrib.postgres.operations.TrigramExtension` migration
  18. operation.
  19. The ``trigram_similar`` lookup can be used on
  20. :class:`~django.db.models.CharField` and :class:`~django.db.models.TextField`::
  21. >>> City.objects.filter(name__trigram_similar="Middlesborough")
  22. ['<City: Middlesbrough>']
  23. ``Unaccent``
  24. ============
  25. .. fieldlookup:: unaccent
  26. The ``unaccent`` lookup allows you to perform accent-insensitive lookups using
  27. a dedicated PostgreSQL extension.
  28. This lookup is implemented using :class:`~django.db.models.Transform`, so it
  29. can be chained with other lookup functions. To use it, you need to add
  30. ``'django.contrib.postgres'`` in your :setting:`INSTALLED_APPS` and activate
  31. the `unaccent extension on PostgreSQL`_. The
  32. :class:`~django.contrib.postgres.operations.UnaccentExtension` migration
  33. operation is available if you want to perform this activation using migrations).
  34. .. _unaccent extension on PostgreSQL: https://www.postgresql.org/docs/current/static/unaccent.html
  35. The ``unaccent`` lookup can be used on
  36. :class:`~django.db.models.CharField` and :class:`~django.db.models.TextField`::
  37. >>> City.objects.filter(name__unaccent="México")
  38. ['<City: Mexico>']
  39. >>> User.objects.filter(first_name__unaccent__startswith="Jerem")
  40. ['<User: Jeremy>', '<User: Jérémy>', '<User: Jérémie>', '<User: Jeremie>']
  41. .. warning::
  42. ``unaccent`` lookups should perform fine in most use cases. However, queries
  43. using this filter will generally perform full table scans, which can be slow
  44. on large tables. In those cases, using dedicated full text indexing tools
  45. might be appropriate.