我有下一个查询。
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)]
还有这个。
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)
)))
这与SQLAlchemy无关。这会将列表解压缩为逗号分隔的参数,并且是Python的一项出色功能。正式将其称为“单星”运算符,但通常将其称为“ splat”运算符。这:
a = [1, 2, 3]
something(*a)
等效于此:
something(1, 2, 3)
因此,在您的第一个示例中,它正在执行列表推导[Item.name.like('%{0}%'.format(s)) for s in words]
,然后将其参数打包到or_
调用中。
有关此运算符的更多信息,请参阅Python文档。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句