Django中的独特模型区域和区分大小写(postgres) [英] Unique model field in Django and case sensitivity (postgres)

查看:227
本文介绍了Django中的独特模型区域和区分大小写(postgres)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下情况: -



假设我的应用程序允许用户在
国家/地区创建州/省。为了清楚起见,我们仅考虑ASCII字符



在美国,用户可以创建名为Texas的状态。如果这个应用程序
正在内部使用,我们假设用户不在乎,如果它是
拼写texas或Texas或teXas



但是重要的是,如果
Texas已经在数据库中,系统应该阻止创建texas。



如果模型是以下:

 类状态(models.Model):
name = models.CharField(max_length = 50,unique = True)

在postgres中,唯一性将区分大小写;也就是说,postgres
将允许用户创建德克萨斯州和德克萨斯州,因为
被认为是唯一的。



在这种情况下做到了防止这种行为。
一直是为Django和
Postgres提供案例 - insenstitive 唯一性



现在我在做以下内容以防止创建case-
不敏感的重复。

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

class Meta:
model = State
/ pre>

有几种情况我将不得不做这个检查,我不想在任何地方编写类似的iexact检查。



只是想知道是否有内置或
更好的方式? db_type也许会有帮助吗?可能还有一些其他解决方案存在?

解决方案

您可以定义从 models.CharField
此字段可以检查重复的值,忽略大小写。



自定义字段文档在这里 http://docs.djangoproject.com/en/dev/howto/custom-model-fields/



查看 http://code.djangoproject.com/browser/django/trunk/django/db/models/fields/files.py ,了解如何通过对现有字段进行子类化来创建自定义字段的示例



您可以使用PostgreSQL的citext模块 https://www.postgresql.org/docs/current/static/citext.html



如果您使用此模块,自定义字段可以将db_type定义为PostgreSQL数据库的CITEXT。



这将导致案例不敏感自定义字段中唯一值的比较。


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?

解决方案

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.

这篇关于Django中的独特模型区域和区分大小写(postgres)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆