我有下一个查询.
item = [item.export_simple()
for item in session.query(Item)
.filter(and_(
Item.companyId == company_id,
or_(
True if search == "" else None,
or_(*[Item.name.like('%{0}%'.format(s)) for s in words]),
or_(*[Item.code.like('%{0}%'.format(s)) for s in words])
))).order_by(Item.name)]
Run Code Online (Sandbox Code Playgroud)
还有这个.
if type == "code":
src = [Item.code.like('%{0}%'.format(s)) for s in words]
elif type == "name":
src = [Item.name.like('%{0}%'.format(s)) for s in words]
session.query(Item)
.filter(and_(
Item.companyId == company_id,
Item.typeItem == item_type,
or_(
True if search == "" else None,
or_(*src)
)))
Run Code Online (Sandbox Code Playgroud)
这与SQLAlchemy无关.这将列表解压缩为逗号分隔的参数,并且是一个出色的Python功能.它被正式称为"单星"运算符,但它通常被称为"splat"运算符.这个:
a = [1, 2, 3]
something(*a)
Run Code Online (Sandbox Code Playgroud)
相当于:
something(1, 2, 3)
Run Code Online (Sandbox Code Playgroud)
因此,在您的第一个示例中,它正在执行列表推导[Item.name.like('%{0}%'.format(s)) for s in words],然后将其参数解压缩到or_调用中.
有关此运算符的更多信息,请参阅Python文档.
| 归档时间: |
|
| 查看次数: |
88 次 |
| 最近记录: |