ORM extensions for filtering. These are all automatically added for the appropriate field types when django_mysql
is in your INSTALLED_APPS
. Note that lookups specific to included model fields <model_fields/index>
are documented with the field, rather than here.
MySQL string comparison has a case-sensitivity dependent on the collation of your tables/columns, as the Django manual describes. However, it is possible to query in a case-sensitive manner even when your data is not stored with a case-sensitive collation, using the BINARY
keyword. The following lookup adds that capability to the ORM for ~django.db.fields.CharField
, ~django.db.fields.TextField
, and subclasses thereof.
Exact, case-sensitive match for character columns, no matter the underlying collation:
>>> Author.objects.filter(name__case_exact="dickens")
[]
>>> Author.objects.filter(name__case_exact="Dickens")
[<Author: Dickens>]
MySQL implements the Soundex algorithm with its SOUNDEX
function, allowing you to find words sounding similar to each other (in English only, regrettably). These lookups allow you to use that function in the ORM and are added for ~django.db.fields.CharField
and ~django.db.fields.TextField
.
Match a given soundex string:
>>> Author.objects.filter(name__soundex="R163")
[<Author: Robert>, <Author: Rupert>]
SQL equivalent:
SELECT ... WHERE SOUNDEX(`name`) = 'R163'
Match the SOUNDEX
of the given string:
>>> Author.objects.filter(name__sounds_like="Robert")
[<Author: Robert>, <Author: Rupert>]
SQL equivalent:
SELECT ... WHERE `name` SOUNDS LIKE 'Robert'