the user returns a string where is parse to apply the filter in sqlalchemy. but the numbers of parameters are unknown. eg:
def fun_name(user_id, value):
return db.query(table)
.filter(or_(table.column_name.like('%'+value+'%')))
this would work if the value is a string where i could apply a filter with that value.
But now I have this situation:
def fun_name(user_id, values):
values_lst = values.split(',')
return db.query(table)
.filter(or_(table.column_name.like('%'+ values_lst[0] +'%'),
table.column_name.like('%'+ values_lst[1] +'%')))
This works only if the split has two parameters, But how do I handle, if I don't know exactly the size of the list in prior?
Is there a generic way to parse this?
Thanks to my Friend, who saved my day with the answer. The solution is
def fun_name(user_id, values):
values_lst = values.split(',')
return db.query(table)
.filter(or_(*[table.column_name.like(f'%{val}%') for val in values_lst]))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.