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条回答
一纸荒年 Trace。
2楼-- · 2019-01-18 02:45

Adding onto what Rasmuj said, escape any user-input like so

import re
result = Name.objects.filter(name__iregex=r'(' + '|'.join([re.escape(n) for n in a]) + ')')
查看更多
登录 后发表回答