Case-insensitve unique = True

32 views
Skip to first unread message

Mike Dewhirst

unread,
Jan 23, 2020, 9:36:29 PM1/23/20
to Django users
What is the best way to ensure a charfield is case-insensitively unique?

Currently I'm using a query with __iexact=True but I wonder whether
there is a better way.

Thanks

Mike


Simon Charette

unread,
Jan 23, 2020, 11:20:25 PM1/23/20
to Django users
If you are using PostgreSQL I'd suggest using a CIText field[0] with a unique
constraint[1].

If you are *not* using PostgreSQL your best choice is likely to emulate the
upcoming expression[3] support for UniqueConstraint(Lower('field')) by using
a RunSQL operation that performs the appropriate DDL assuming your backend
supports it.

Best,
Simon

maninder singh Kumar

unread,
Jan 24, 2020, 2:13:57 AM1/24/20
to django...@googlegroups.com
I wonder at that.  When I develop a model does the CharField have to be specified case insensitive at that level.

For instance :  A search can be case insensitive and iexact is the best way.
 
               
 


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/49ae5424-f079-d4eb-bc6d-aa5231efa773%40dewhirst.com.au.

Mike Dewhirst

unread,
Jan 24, 2020, 2:48:24 AM1/24/20
to django...@googlegroups.com
On 24/01/2020 3:20 pm, Simon Charette wrote:
> If you are using PostgreSQL I'd suggest using a CIText field[0] with a
> unique
> constraint[1].

Surely everyone uses PostgreSQL, no?

That [0] is identical in Django 2.2 and it looks like I should migrate
my models.CharField to django.contrib.postgresfield.CICharField

I'm already using [1] in Django 2.2

Thank you Simon you have made my day :-)

Cheers

Mike

>
> If you are *not* using PostgreSQL your best choice is likely to
> emulate the
> upcoming expression[3] support for UniqueConstraint(Lower('field')) by
> using
> a RunSQL operation that performs the appropriate DDL assuming your backend
> supports it.
>
> Best,
> Simon
>
> [0]
> https://docs.djangoproject.com/en/3.0/ref/contrib/postgres/fields/#citext-fields
> [1] https://docs.djangoproject.com/en/3.0/ref/models/fields/#unique
> [3] https://github.com/django/django/pull/11929
>
> Le jeudi 23 janvier 2020 21:36:29 UTC-5, Mike Dewhirst a écrit :
>
> What is the best way to ensure a charfield is case-insensitively
> unique?
>
> Currently I'm using a query with __iexact=True but I wonder whether
> there is a better way.
>
> Thanks
>
> Mike
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/967c8a27-a8ec-42fc-abf5-bda76fbb5ef9%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/967c8a27-a8ec-42fc-abf5-bda76fbb5ef9%40googlegroups.com?utm_medium=email&utm_source=footer>.

Mike Dewhirst

unread,
Jan 24, 2020, 3:05:35 AM1/24/20
to django...@googlegroups.com
On 24/01/2020 6:12 pm, maninder singh Kumar wrote:
> I wonder at that.  When I develop a model does the CharField have to
> be specified case insensitive at that level.

That is what Simon advised and I think it is exactly what I want.

>
> For instance :  A search can be case insensitive and iexact is the
> best way.

Searching isn't the problem. I don't want duplicate names differing only
in the case and I want that to be enforced at the model level at
instance creation time and also later if the name is edited.

I have been using iexact to "manually" enforce case insensitive-unique
names.

Cheers

Mike


> --
>
> Maninder Kumar
> http://about.me/maninder.s.kumar
>
> <http://about.me/maninder.s.kumar?promo=email_sig>
>
>
>
> On Fri, Jan 24, 2020 at 8:06 AM Mike Dewhirst <mi...@dewhirst.com.au
> <mailto:mi...@dewhirst.com.au>> wrote:
>
> What is the best way to ensure a charfield is case-insensitively
> unique?
>
> Currently I'm using a query with __iexact=True but I wonder whether
> there is a better way.
>
> Thanks
>
> Mike
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it,
> send an email to django-users...@googlegroups.com
> <mailto:django-users%2Bunsu...@googlegroups.com>.
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CABOHK3RTRmRwyMsgdryp50vVbBeopqBcu2CCQdrY9aJySjwkpg%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CABOHK3RTRmRwyMsgdryp50vVbBeopqBcu2CCQdrY9aJySjwkpg%40mail.gmail.com?utm_medium=email&utm_source=footer>.

gaurav jain

unread,
Jan 24, 2020, 3:09:36 AM1/24/20
to django...@googlegroups.com
Hi,
I think you can save a processed version of the text and apply a unique constraint. 
If you want save the original text then save the non processed test as well.
Kind regards
Gaurav Jain


To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/8c6f13f3-1e27-cd55-6ee8-51f0a1dcd039%40dewhirst.com.au.
Reply all
Reply to author
Forward
0 new messages