【SQLalchemy】filter_by can't use the 'like' query

use filter instead:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
def __filter(self, order=None, order_type="asc", **kwargs):
try:
query_cond = {}
query_cond_like = []
for k, v in kwargs.items():
# 11.8 add like support
if '__contains' in k:
k = k.replace("__contains", "")
query_cond_like.append(self.table.__dict__[k].like("%"+v+"%"))
continue
if type(v) == type(u'unicode'):
query_cond[k] = v.encode('utf8')
else:
query_cond[k] = v
if order == None:
tmp = self.session.query(self.table).filter(*query_cond_like).filter_by(**query_cond)
else:
tmp = self.session.query(self.table).order_by(eval(order_type)(self.table.__dict__[order])).filter(*query_cond_like).filter_by(**query_cond)
return tmp #_sql
except Exception as e:
logging.error("sql search error kwars:%s" % kwargs)
logging.error(e)
return None