postgresql - Unique model field in Django and case sensitivity (postgres) -


Consider the following situation: -

Suppose that my app users should state / Country. Just for clarity, we are only considering ASCII characters here.

In the US, a user can create a state called "Texas". If this app is being used internally, then assume that the user is not spelled "Texas" or "Texas" or "Texans", but it is important that the system be "Texas" in the "Texas" database Is already there.

If the model is like the following:

  class state (models.Model): name = models.CharField (max_length = 50, exclusive = true)  

Specification cases in postgres will be sensitive; That is, Postgrey will allow the user to create both "Texas" and "Texas" because they are considered unique.

What can be done in this situation to prevent such behavior. How is known about providing insensitive exclusivity with the degego and postgrid

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

  class CreateStateForm (forms.ModelForm): def clean_name (auto): name = self.cleaned_data ['name'] Try: State.objects.get (name__iexact = name) Excluding ObjectDoesNotExist: Return names increase the forms. ValidityError ('State already exists.') Category Meta: Model = State  

There are many cases where I have to check this and I'm not eager to write it everywhere Test check

Just wondering if there is any built-in or better way? Maybe db_type will help? Maybe some other solution exists?

You can define custom model fields derived from the model. . This field can examine the duplicate values, ignore the case.

There is a custom field document here

See an example of how to sub-classize a current field and create a custom field. / P>

You can use PostgreSQL's citext module

If you use this module, define Custom Field "DB_type" as the CITEX for PostgreSQL databases Could.

This will inspire the insensitive comparison of unique values ​​in custom fields.


Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

php - Multiple Select with Explode: only returns the word "Array" -

php - jQuery AJAX Post not working -