Unique model field in Django and case sensitivity (postgres)

前端 未结 10 755
时光取名叫无心
时光取名叫无心 2020-12-13 13:37

Consider the following situation: -

Suppose my app allows users to create the states / provinces in their country. Just for clarity, we are considering only ASCII ch

10条回答
  •  爱一瞬间的悲伤
    2020-12-13 14:12

    Explicit steps for Mayuresh's answer:

    1. in postgres do: CREATE EXTENSION citext;

    2. in your models.py add:

      from django.db.models import fields
      
      class CaseInsensitiveTextField(fields.TextField):
          def db_type(self, connection):
              return "citext"
      

      reference: https://github.com/zacharyvoase/django-postgres/blob/master/django_postgres/citext.py

    3. in your model use: name = CaseInsensitiveTextField(unique=True)

提交回复
热议问题