Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 504925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:32:46+00:00 2026-05-13T06:32:46+00:00

Consider the following situation: – Suppose my app allows users to create the states

  • 0

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 characters
here.

In the US, a user could create the state called “Texas”. If this app
is being used internally, let’s say the user doesn’t care if it is
spelled “texas” or “Texas” or “teXas”

But importantly, the system should prevent creation of “texas” if
“Texas” is already in the database.

If the model is like the following:

class State(models.Model):
    name = models.CharField(max_length=50, unique=True)

The uniqueness would be case-sensitive in postgres; that is, postgres
would allow the user to create both “texas” and “Texas” as they are
considered unique.

What can be done in this situation to prevent such behavior. How does
one go about providing case-insenstitive uniqueness with Django and
Postgres

Right now I’m doing the following to prevent creation of case-
insensitive duplicates.

class CreateStateForm(forms.ModelForm):
    def clean_name(self):
        name = self.cleaned_data['name']
        try:
            State.objects.get(name__iexact=name)
        except ObjectDoesNotExist:
            return name
        raise forms.ValidationError('State already exists.')

    class Meta:
        model = State

There are a number of cases where I will have to do this check and I’m not keen on having to write similar iexact checks everywhere.

Just wondering if there is a built-in or
better way? Perhaps db_type would help? Maybe some other solution exists?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T06:32:46+00:00Added an answer on May 13, 2026 at 6:32 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following situation: I'm using the Application object in ASP.NET to store a
consider the following code: #define MAX_COUNT 4 static int foo(int w[MAX_COUNT]) { int *p
please consider the following scenario for .net 2.0: I have an event that is
Consider following snippet code for xml. <rootnode> <child id=child1 ><![CDATA[child 1]]></child> <child id=child2 ><![CDATA[child
Consider following array $details = array( array('lname'=>'A', 'fname'=>'P','membkey'=>700,'head'=>'y'), array('lname'=>'B', 'fname'=>'Q','membkey'=>540,'head'=>'n'), array('lname'=>'C', 'fname'=>'R','membkey'=>700,'head'=>'n'), array('lname'=>'D', 'fname'=>'S','membkey'=>540,'head'=>'y'),
I have the following class which I'm fighting over how to implement. The question
I've been running into the following case repeatedly lately, either I need to apply
Let's first consider a simple scenario ( see complete source on ideone.com ): import
Is there a (practical) way to by-pass the normal (virtual) constructor calling order? Example:
In C++, is a const method returning a pointer to a non-const object considered

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.