from django.db.models.query import Q def superSearch(model, fields, matches, strings): """ Designed to lessen the code needed to run complex searches with ORed filters. Model: the model being queried. fields: an iterable containing string names of fields to query. match: an iterable containing strings of what type of Django lookup to apply to those fields. strings: an iterable containing strings to be matched. """ queries = [] for field in fields: for string in strings: for match in matches: kwargs = {'%s__%s' % (field, match): string} queries.append(Q(**kwargs)) q = Q() for query in queries: q = q | query return model.objects.filter(q)