[Django]-Case insensitive field in Django

3๐Ÿ‘

You can have another field which is basically the slug of the name. Infact, I believe it is not a good idea to have the name as a unique field (but i should clarify that i do not know your requirement).

Basically, the validation on the slug field ensures uniqueness. Also, you can just keep the slug field hidden from all forms, etc..

Example:

>>> from django.utils.text import slugify
>>> slugify(u"ล›tack Overflow")
u'stack-overflow'
>>> slugify(u"stack Overflow")
u'stack-overflow'
>>> slugify(u"stack  Overflow")
u'stack-overflow'
>>> slugify(u"stack \t Overflow")
u'stack-overflow'
>>> slugify(u"stack \n Overflow")
u'stack-overflow'

A few of these combinations map to the same slug โ€“ which ensures uniqueness for the broad usecases.

๐Ÿ‘คkarthikr

3๐Ÿ‘

An idea would be to implement a case insensitive unique constraint on the field.

Possible answers in:

๐Ÿ‘คn__o

0๐Ÿ‘

Just save both full_name and clean_full_name in the database, and make clean_full_name unique.

You can validate the full_name field by putting the validation code in a property setter. Have a look at this blog for details.

๐Ÿ‘คRob

Leave a comment