I'm trying to use list comprehension with or_. I already found a post that showed how to do it with only checking one column, but I'm unsure how to check two columns simultaneously with a list. I tried something like this:
q = q.filter(or_((model.column.contains(word), model.column2.contains(word))for word in search))
This gives me a bad request though. Any advice on how to search simultaneously using a list would be appreciated!
From what I understand you need to unpack the conditions into positional arguments of or_()
:
columns = [model.column, model.column2]
conditions = [column.contains(word) for word in search for column in columns]
q = q.filter(or_(*conditions))
Assuming you want to "or" every contains for every word and for all given columns.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With