Created
August 15, 2021 13:20
-
-
Save hleroy/2f3c6b00f284180da10ed9d20bf9240a to your computer and use it in GitHub Desktop.
How to use Django 3.2 CreateCollation and db_collation to implement a case-insensitive Charfield with Postgres > 12
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# According to Django documentation, it’s preferable to use non-deterministic collations | |
# instead of the citext extension for Postgres > 12. | |
# Example migation to create the case insensitive collation | |
class Migration(migrations.Migration): | |
operations = [ | |
CreateCollation( | |
'case_insensitive', | |
provider='icu', | |
locale='und-u-ks-level2', | |
deterministic=False | |
) | |
] | |
# Example model using the new db_collation parameter introduced with Django 3.2 | |
class Tag(models.Model): | |
name = models.CharField(max_length=50, db_collation='case_insensitive') | |
class Meta: | |
ordering = ['name'] | |
def __str__(self): | |
return self.name |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment