Here is a solution that does not require DB data. It also does filtering on the side of the database processor, which means much more performance than iterating over objects.all() .
def case_insensitive_in_filter(fieldname, iterable): """returns Q(fieldname__in=iterable) but case insensitive""" q_list = map(lambda n: Q(**{fieldname+'__iexact': n}), iterable) return reduce(lambda a, b: a | b, q_list)
Another effective solution is to use an additional version with the rather portable raw-SQL lower() function:
MyModel.objects.extra( select={'lower_' + fieldname: 'lower(' + fieldname + ')'} ).filter('lover_' + fieldname + '__in'=[x.lower() for x in iterable])
source share