Unique model field in Django and case sensitivity (postgres)

前端 未结 10 750
时光取名叫无心
时光取名叫无心 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:22

    You could define a custom model field derived from models.CharField. This field could check for duplicate values, ignoring the case.

    Custom fields documentation is here http://docs.djangoproject.com/en/dev/howto/custom-model-fields/

    Look at http://code.djangoproject.com/browser/django/trunk/django/db/models/fields/files.py for an example of how to create a custom field by subclassing an existing field.

    You could use the citext module of PostgreSQL https://www.postgresql.org/docs/current/static/citext.html

    If you use this module, the the custom field could define "db_type" as CITEXT for PostgreSQL databases.

    This would lead to case insensitive comparison for unique values in the custom field.

提交回复
热议问题