有什么办法可以在Django中进行不区分大小写的IN查询吗? [英] Is there any way to do a case-insensitive IN query in Django?

查看:124
本文介绍了有什么办法可以在Django中进行不区分大小写的IN查询吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Django中,几乎所有类型的查询都具有不区分大小写的版本,出现在其中。

Nearly every kind of lookup in Django has a case-insensitive version, EXCEPT in, it appears.

这是一个问题,因为有时我需要进行查询

This is a problem because sometimes I need to do a lookup where I am certain the case will be incorrect.

Products.objects.filter(code__in=[user_entered_data_as_list])

有什么我可以做的处理吗?人们有没有想办法解决此问题?

Is there anything I can do to deal with this? Have people come up with a hack to work around this issue?

推荐答案

我通过将MySQL数据库本身设置为大小写来解决此问题,不敏感。我怀疑Django的人们是否有兴趣将其添加为功能或提供有关如何提供自己的字段查找的文档的信息(假设甚至有可能在不为每个数据库后端提供代码的情况下)

I worked around this by making the MySQL database itself case-insensitive. I doubt that the people at Django are interested in adding this as a feature or in providing docs on how to provide your own field lookup (assuming that is even possible without providing code for each db backend)

这是一种实现方法,很笨重。

Here is one way to do it, admittedly it is clunky.

products = Product.objects.filter(**normal_filters_here)
results = Product.objects.none()
for d in user_entered_data_as_list:
    results |= products.filter(code__iexact=d)

这篇关于有什么办法可以在Django中进行不区分大小写的IN查询吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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