有什么办法可以在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)