Django 在 Postgresql db_collation 字段中搜索

问题描述 投票:0回答:1

我正在使用 Django 和 DRF searchFilter,它允许我将字段添加到视图的“search_fields”以允许在其中进行搜索。 Django 版本是 3.2(可能会升级到 4.2),DRF 版本是 3.12.2。 最近,我添加了一个类型为

CharField
的新字段,其 db_collation 为
case_insensitive
。我需要该字段不区分大小写,并且我知道 Postgres 将停止支持
CI
字段,建议使用 db_collation。它确实有效,直到我想支持该领域的搜索。将新字段添加到视图的“search_fields”并尝试搜索后,我收到此错误消息 -

 File ".../lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params) 
django.db.utils.NotSupportedError: nondeterministic collations are not supported for LIKE

有没有预先准备好的解决方案? 我找不到任何东西,而且我发现 Postgres 强制使用 db_collation 但不支持 LIKE 很奇怪。

这是模型中的字段定义 -

custom_field = models.CharField(db_collation="case_insensitive", db_index=True, max_length=100, null=True)

这是排序规则定义 -

[
    CreateCollation(
        "case_insensitive",
        provider="icu",
        locale="und-u-ks-level2-kn-true",
        deterministic=False,
    ),
]
django postgresql collation
1个回答
0
投票

根据here的讨论,这已在 Django 4.2 中修复。 Django 3(确切地说是 3.2.x)的解决方法是子类化 Postgres SchemaEditor 以覆盖其

_create_like_index_sql
方法并在数据库条目中使用调整后的后端。

如果 Postgres 数据库上存在

varchar_pattern_ops
索引,更改字段可能还不够,可能需要重新创建。

希望这有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.