I need to search for some data in the database using django ORM and this should be searching by all columns. The problem is that I do not know what to search before I get into the function as a parameter.
This is the source code:
search = request.GET.get('search_value')
#row_to_search is turple i get to function
#it could be different at every searching
#it looks like here:
rows_to_search = ('name', 'age', 'salary')
I need to search in my database for that rows, something like this:
query = Mymodel.objects.filter(Q(name__iregex=search) | \
Q(age__iregex=search) | \
Q(salary__iregex=search))
How do I make the code if I will get more rows to search for?
I tried something like this but it seams it is not working at all:
Q_filter = map(lambda q: 'Q('+q+')', map(lambda s: s+'__regex='+search, rows_to_search))
searchline = '|'.join(Q_filter)
I can not call model.objects.filter(searchline)
, because it is an String.
Is it possible to do something here, or it is the architecture wrong? Should I make a custom model manager?