Skip to content

Instantly share code, notes, and snippets.

@hleroy
Created August 15, 2021 13:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hleroy/2f3c6b00f284180da10ed9d20bf9240a to your computer and use it in GitHub Desktop.
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
# 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