如果这对某人有用,这就是我最终要做的事情:
from flask import requestclass Parser(object): sep = ';' # ... def filter_query(self, query): model_class = self._get_model_class(query) # returns the query's Model raw_filters = request.args.getlist('filter') for raw in raw_filters: try: key, op, value = raw.split(self.sep, 3) except ValueError: raise APIError(400, 'Invalid filter: %s' % raw) column = getattr(model_class, key, None) if not column: raise APIError(400, 'Invalid filter column: %s' % key) if op == 'in': filt = column.in_(value.split(',')) else: try: attr = filter( lambda e: hasattr(column, e % op), ['%s', '%s_', '__%s__'] )[0] % op except IndexError: raise APIError(400, 'Invalid filter operator: %s' % op) if value == 'null': value = None filt = getattr(column, attr)(value) query = query.filter(filt) return query
这涵盖了所有SQLAlchemy列比较器:
eq
对于==
lt
对于<
ge
对于>=
in
对于in_
like
对于like
- 等等
详尽列表及其相应名称可在此处找到。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)