gpt4 book ai didi

Django自定义字段-自动添加COLLATE到查询

转载 作者:行者123 更新时间:2023-12-02 09:41:24 25 4
gpt4 key购买 nike

我正在尝试创建一个自定义字段,它将自动将 COLLATE 信息添加到 SQL 查询的 WHERE 部分中:

class IgnoreDiacriticsField(models.TextField):

def get_prep_lookup(self, lookup_type, value):
if lookup_type == 'exact':
return ' "' + self.get_prep_value(value) + '" COLLATE utf8_general_ci'

当我执行这样的查询时:

result = ModelClass.objects.filter(field='value')

那么即使查询 (print result.query) 有效并且匹配多行,也找不到任何内容。我做错了什么吗?

我添加排序规则信息的原因是我想对这些字段执行查询并忽略任何变音符号。

最佳答案

您是否使用 MySQL 1.2.1p2?来自 Django 文档

If you're using MySQLdb 1.2.1p2, Django's standard CharField class will return unicode strings even with utf8_bin collation. However, TextField fields will be returned as an array.array instance (from Python's standard array module). There isn't a lot Django can do about that, since, again, the information needed to make the necessary conversions isn't available when the data is read in from the database. This problem was fixed in MySQLdb 1.2.2, so if you want to use TextField with utf8_bin collation, upgrading to version 1.2.2 and then dealing with the bytestrings (which shouldn't be too difficult) as described above is the recommended solution.

关于Django自定义字段-自动添加COLLATE到查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3154383/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com