diff options
| author | Brad Melin <melinbrad@gmail.com> | 2017-02-21 08:43:38 +0200 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2017-03-14 19:58:56 -0400 |
| commit | b625907a79bb1336cbc54231bdf7cace25fecaf7 (patch) | |
| tree | 824ee84d7123c31c87860e137dc48806be5b6cb8 /docs/ref | |
| parent | 3f64fd2f75342fa061338d75916c08b98f0c7797 (diff) | |
Fixed #27834 -- Added StrIndex database function.
Diffstat (limited to 'docs/ref')
| -rw-r--r-- | docs/ref/models/database-functions.txt | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/docs/ref/models/database-functions.txt b/docs/ref/models/database-functions.txt index 18d547d906..21b6f897ac 100644 --- a/docs/ref/models/database-functions.txt +++ b/docs/ref/models/database-functions.txt @@ -237,6 +237,38 @@ Usage example:: ``Now()`` uses ``STATEMENT_TIMESTAMP`` instead. If you need the transaction timestamp, use :class:`django.contrib.postgres.functions.TransactionNow`. +``StrIndex`` +============ + +.. class:: StrIndex(expression, substring, **extra) + +.. versionadded:: 2.0 + +Returns a positive integer corresponding to the 1-indexed position of the +first occurrence of ``substring`` inside another string, or 0 if the substring +is not found. + +Usage example:: + + >>> from django.db.models.functions import StrIndex + >>> Author.objects.create(name='Margaret Smith') + >>> Author.objects.create(name='Smith, Margaret') + >>> Author.objects.create(name='Margaret Jackson') + >>> authors = Author.objects.annotate( + ... smith_index=StrIndex('name', 'Smith')).order_by('smith_index') + >>> authors.first().smith_index + 0 + >>> authors = Author.objects.annotate( + ... smith_index=StrIndex('name', 'Smith')).filter(smith_index__gt=0) + <QuerySet [<Author: Margaret Smith>, <Author: Smith, Margaret>]> + +.. warning:: + + In MySQL, a database table's :ref:`collation<mysql-collation>` determines + whether string comparisons (such as the ``expression`` and ``substring`` of + this function) are case-sensitive. Comparisons are case-insensitive by + default. + ``Substr`` ========== |
