简体   繁体   English

SQLAlchemy 过滤器 - select 全部?

[英]SQLAlchemy filter - select all?

Is there a wild card character for selecting everything with SQLAlchemy's "filter"?是否有通配符用于使用 SQLAlchemy 的“过滤器”选择所有内容?

var = '*'
session.query().filter(results.name == var)

Where '*' is anything or all and 'var' changes based on the user's selection.其中“*”是任何或全部,“var”会根据用户的选择而变化。 When the user selects all, I would like this to return as if the filter was not applied.当用户全选时,我希望它返回好像未应用过滤器一样。

I may be approaching this wrong in which case here is an explanation of my use case.我可能正在接近这个错误,在这种情况下,这里是我的用例的解释。 I am using flask and plotly to create a simple webpage where the user can select from 3 drop down boxes that subsets a data set and creates a line plot. I am using flask and plotly to create a simple webpage where the user can select from 3 drop down boxes that subsets a data set and creates a line plot. One of the options in each of the 3 selection boxes is "All", as in do not apply the filter. 3 个选择框中的选项之一是“全部”,如不应用过滤器。

Using filter使用filter

condition = results.name == var
if want == 'all' or want == '*':
    condition = True
session.query().filter(condition)

Using where使用where

condition = results.name == var
if want == 'all' or want == '*':
    condition = True
session.query().where(condition)

Using expression with where where使用expression

from sqlalchemy import true
session.query().filter(true() if user_selection == '*' else results.name == var)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM