【问题标题】:How to use filter in sqlalchemy with three parameter如何在具有三个参数的 sqlalchemy 中使用过滤器
【发布时间】:2018-04-04 22:21:55
【问题描述】:

我有这样的代码:

price = Price.query.filter(and_(Price.product_id == data['product_id'], Price.min_qty >= data['quantity'], Price.max_qty <= data['quantity'])).first()

它不起作用并给出错误,但如果我只使用2个参数,那很好

price = Price.query.filter(and_(Price.product_id == data['product_id'], Price.min_qty >= data['quantity'])).first()

为什么我不能有三个参数?我到处搜索并没有找到答案。我尝试实现 SELECT * FROM price WHERE product_id = 'product_id' AND min_qty = 'quantity' AND max_qty = 'quantity'

谢谢

【问题讨论】:

  • 把你得到的错误告诉我们?
  • @senaps 它没有给出错误,但是当我调用 price.price 时没有显示任何内容
  • 我认为我的逻辑是错误的

标签: python postgresql flask sqlalchemy


【解决方案1】:

你可以试试这个:

query = Price.query.filter(Price.product_id == data['product_id'])
query = query.filter(Price.min_qty >= data['quantity'])
price = query.filter(Price.max_qty <= data['quantity']).first()

或者使用 and 来减少一行。

【讨论】:

  • 谢谢,问题是我的逻辑是错误的,我发现你的代码比一行更有趣
猜你喜欢
  • 2011-10-30
  • 2013-08-18
  • 2013-04-20
  • 2015-12-08
  • 2018-04-11
  • 2014-11-09
  • 1970-01-01
  • 1970-01-01
  • 2019-08-09
相关资源
最近更新 更多