1 回答
TA贡献1856条经验 获得超17个赞
这是一段基于一组“动态”过滤器运行查询的代码。
filters = []
# this is an example:
inputfield = {
"inputVendorName": "J",
"inputProductName": "Pen",
"MinPrice": 10
}
if 'inputVendorName' in inputfield:
filters.append(Vendor.vendor_name.contains(inputfield["inputVendorName"]))
if 'inputProductName' in inputfield:
filters.append(Product.product_name.contains(inputfield["inputProductName"]))
if 'MinPrice' in inputfield:
filters.append(Sale.price > inputfield["MinPrice"])
base_query = session.query(Customer, Product, Vendor, Sale).filter(
Sale.customer_id == Customer.customer_id, Vendor.vendor_id == Product.vendor_id, Sale.product_id == Product.product_id)
for res in base_query.filter(*filters).all():
print(res)
添加回答
举报