Django query case-insensitive list match

2019-01-18 02:08发布

I have a list of names that I want to match case insensitive, is there a way to do it without using a loop like below?

a = ['name1', 'name2', 'name3']
result = any([Name.objects.filter(name__iexact=name) for name in a])

7条回答
Luminary・发光体
2楼-- · 2019-01-18 02:22

Unfortunatley, there are no __iin field lookup. But there is a iregex that might be useful, like so:

result = Name.objects.filter(name__iregex=r'(name1|name2|name3)')

or even:

a = ['name1', 'name2', 'name3']
result = Name.objects.filter(name__iregex=r'(' + '|'.join(a) + ')')

Note that if a can contain characters that are special in a regex, you need to escape them properly.

NEWS: In Djano 1.7 it is possible to create your own lookups, so you can actually use filter(name__iin=['name1', 'name2', 'name3']) after proper initialization. See https://docs.djangoproject.com/en/1.7/ref/models/lookups/ for details.

查看更多
We Are One
3楼-- · 2019-01-18 02:29

I am expanding Exgeny idea into an two liner.

import functools
Name.objects.filter(functools.reduce(lambda acc,x: acc | Q(name_iexact=x)), names, Q()))
查看更多
Deceive 欺骗
4楼-- · 2019-01-18 02:33

Here is an example of custom User model classmethod to filter users by email case-insensitive

from django.db.models import Q

@classmethod
def get_users_by_email_query(cls, emails):
    q = Q()
    for email in [email.strip() for email in emails]:
        q = q | Q(email__iexact=email)
    return cls.objects.filter(q)
查看更多
乱世女痞
5楼-- · 2019-01-18 02:38

In Postgresql you could try creating a case insensitive index as described here:

https://stackoverflow.com/a/4124225/110274

Then run a query:

from django.db.models import Q
name_filter = Q()
for name in names:
    name_filter |= Q(name__iexact=name)
result = Name.objects.filter(name_filter)

Index search will run faster than the regex matching query.

查看更多
爱情/是我丢掉的垃圾
6楼-- · 2019-01-18 02:38

Keep in mind that at least in MySQL you have to set utf8_bin collation in your tables to actually make them case sensitive. Otherwise they are case preserving but case insensitive. E.g.

>>> models.Person.objects.filter(first__in=['John', 'Ringo'])
[<Person: John Lennon>, <Person: Ringo Starr>]
>>> models.Person.objects.filter(first__in=['joHn', 'RiNgO'])
[<Person: John Lennon>, <Person: Ringo Starr>]

So, if portability is not crucial and you use MySQL you may choose to ignore the issue altogether.

查看更多
▲ chillily
7楼-- · 2019-01-18 02:45

Another way to this using django query functions and annotation

from django.db.models.functions import Lower
Record.objects.annotate(name_lower=Lower('name')).filter(name_lower__in=['two', 'one']
查看更多
登录 后发表回答