本文整理汇总了Python中pyramid_sqlalchemy.Session.filter_by方法的典型用法代码示例。如果您正苦于以下问题:Python Session.filter_by方法的具体用法?Python Session.filter_by怎么用?Python Session.filter_by使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyramid_sqlalchemy.Session
的用法示例。
在下文中一共展示了Session.filter_by方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_collection
# 需要导入模块: from pyramid_sqlalchemy import Session [as 别名]
# 或者: from pyramid_sqlalchemy.Session import filter_by [as 别名]
#.........这里部分代码省略.........
query.
:raises JHTTPBadRequest: When ``sqlalchemy.exc.InvalidRequestError``
or ``sqlalchemy.exc.IntegrityError`` errors happen during DB
query.
"""
log.debug('Get collection: {}, {}'.format(cls.__name__, params))
params.pop('__confirmation', False)
__strict = params.pop('__strict', True)
_item_request = params.pop('_item_request', False)
_sort = _split(params.pop('_sort', []))
_fields = _split(params.pop('_fields', []))
_limit = params.pop('_limit', None)
_page = params.pop('_page', None)
_start = params.pop('_start', None)
query_set = params.pop('query_set', None)
_count = '_count' in params
params.pop('_count', None)
_explain = '_explain' in params
params.pop('_explain', None)
__raise_on_empty = params.pop('__raise_on_empty', False)
if query_set is None:
query_set = Session().query(cls)
# Remove any __ legacy instructions from this point on
params = dictset({
key: val for key, val in params.items()
if not key.startswith('__')
})
iterables_exprs, params = cls._pop_iterables(params)
params = drop_reserved_params(params)
if __strict:
_check_fields = [
f.strip('-+') for f in list(params.keys()) + _fields + _sort]
cls.check_fields_allowed(_check_fields)
else:
params = cls.filter_fields(params)
process_lists(params)
process_bools(params)
# If param is _all then remove it
params.pop_by_values('_all')
try:
query_set = query_set.filter_by(**params)
# Apply filtering by iterable expressions
for expr in iterables_exprs:
query_set = query_set.from_self().filter(expr)
_total = query_set.count()
if _count:
return _total
# Filtering by fields has to be the first thing to do on
# the query_set!
query_set = cls.apply_fields(query_set, _fields)
query_set = cls.apply_sort(query_set, _sort)
if _limit is not None:
_start, _limit = process_limit(_start, _page, _limit)
query_set = query_set.offset(_start).limit(_limit)
if not query_set.count():
msg = "'%s(%s)' resource not found" % (cls.__name__, params)
if __raise_on_empty:
raise JHTTPNotFound(msg)
else:
log.debug(msg)
except DataError as ex:
if _item_request:
msg = "'{}({})' resource not found".format(
cls.__name__, params)
raise JHTTPNotFound(msg, explanation=ex.message)
else:
raise JHTTPBadRequest(str(ex), extra={'data': ex})
except (InvalidRequestError,) as ex:
raise JHTTPBadRequest(str(ex), extra={'data': ex})
query_sql = str(query_set).replace('\n', '')
if _explain:
return query_sql
log.debug('get_collection.query_set: %s (%s)', cls.__name__, query_sql)
if _fields:
query_set = cls.add_field_names(query_set, _fields)
query_set._nefertari_meta = dict(
total=_total,
start=_start,
fields=_fields)
return query_set