Field Lookups#

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 are documented with the field, rather than here.

Case-sensitive String Comparison#

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 CharField, 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 CharField and 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'