本文整理汇总了Python中pyramid_sqlalchemy.Session.first方法的典型用法代码示例。如果您正苦于以下问题:Python Session.first方法的具体用法?Python Session.first怎么用?Python Session.first使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyramid_sqlalchemy.Session
的用法示例。
在下文中一共展示了Session.first方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: filter_objects
# 需要导入模块: from pyramid_sqlalchemy import Session [as 别名]
# 或者: from pyramid_sqlalchemy.Session import first [as 别名]
def filter_objects(cls, objects, first=False, **params):
""" Perform query with :params: on instances sequence :objects:
:param object: Sequence of :cls: instances on which query should be run.
:param params: Query parameters to filter :objects:.
"""
id_name = cls.pk_field()
ids = [getattr(obj, id_name, None) for obj in objects]
ids = [str(id_) for id_ in ids if id_ is not None]
field_obj = getattr(cls, id_name)
query_set = Session().query(cls).filter(field_obj.in_(ids))
if params:
params['query_set'] = query_set.from_self()
query_set = cls.get_collection(**params)
if first:
first_obj = query_set.first()
if not first_obj:
msg = "'{}({})' resource not found".format(
cls.__name__, params)
raise JHTTPNotFound(msg)
return first_obj
return query_set