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

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

问题描述

考虑以下情况:-

假设我的应用允许用户在他们的应用中创建州/省国家.为清楚起见,我们只考虑 ASCII 字符在这里.

Suppose my app allows users to create the states / provinces in their country. Just for clarity, we are considering only ASCII characters here.

在美国,用户可以创建名为德克萨斯州"的州.如果这个应用正在内部使用,假设用户不关心它是否是拼写为texas"或Texas"或teXas"

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.

如果模型如下:

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

唯一性在 postgres 中区分大小写;也就是说,postgres将允许用户按原样创建德克萨斯州"和德克萨斯州"被认为是独一无二的.

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.

在这种情况下可以做些什么来防止这种行为.如何一方面为 Django 提供大小写不敏感独特性,Postgres

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

在很多情况下我都必须进行此检查,而且我并不热衷于在任何地方编写类似的 iexact 检查.

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.

只是想知道是否有内置或更好的方法?也许 db_type 会有所帮助?也许存在其他解决方案?

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

推荐答案

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

You could define a custom model field derived from models.CharField. This field could check for duplicate values, ignoring the case.

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

Custom fields documentation is here http://docs.djangoproject.com/en/dev/howto/custom-model-fields/

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

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.

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

You could use the citext module of PostgreSQL https://www.postgresql.org/docs/current/static/citext.html

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

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天全站免登陆