lookups.txt 2.3 KB

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