Case insensitive unique model fields in Django?

后端 未结 7 2129
我寻月下人不归
我寻月下人不归 2020-12-02 11:23

I have basically a username is unique (case insensitive), but the case matters when displaying as provided by the user.

I have the following requirements:

7条回答
  •  星月不相逢
    2020-12-02 11:40

    As of Django 1.11, you can use CITextField, a Postgres-specific Field for case-insensitive text backed by the citext type.

    from django.db import models
    from django.contrib.postgres.fields import CITextField
    
    class Something(models.Model):
        foo = CITextField()
    

    Django also provides CIEmailField and CICharField, which are case-insensitive versions of EmailField and CharField.

提交回复
热议问题