【问题标题】:Flask-SQLAlchemy: from selected row get list of column names where value equals somethingFlask-SQLAlchemy:从选定的行获取值等于某物的列名列表
【发布时间】:2020-12-29 17:46:18
【问题描述】:
progress = Progress.query.get_or_404(name)

在获得“progress”对象后,我不想检查每一列是否等于“yes”,而是想要仅包含几个查询之一的列列表。

换句话说,

result = ["foo", "bar"]

“foo”和“bar”列在“progress”行中的值为“yes”。

【问题讨论】:

    标签: python sql sqlite sqlalchemy flask-sqlalchemy


    【解决方案1】:

    到目前为止,我已经用这个列表理解解决了这个问题:

    result = [col for col in progress.__table__.columns.keys() if getattr(progress, col) == "yes"]
    

    如果可能的话,如果有办法只使用一个查询会更好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-07
      • 1970-01-01
      • 2013-04-12
      • 1970-01-01
      • 1970-01-01
      • 2013-01-24
      相关资源
      最近更新 更多