I want to look for a certain string in several fields of a Model in Django. Ideally, it would go something similar to:
keyword = 'keyword'
fields = ['foo', 'bar', 'baz']
results = []
for field in fields:
lookup = "%s__contains"
results.append(Item.objects.filter(lookup=keyword))
Of course this won’t work, as “lookup” can’t be resolved into a field. Is there any other way to do this?
I think there may be a better way to do this with the Django query system. Here’s how to do it your way.
Python allows you to pass dictionaries to be used as argument lists by prefixing them with **.
With a spot of luck, you should be able to do something like this: