简体   繁体   中英

SQLAlchemy filter - select all?

Is there a wild card character for selecting everything with SQLAlchemy's "filter"?

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

Where '*' is anything or all and 'var' changes based on the user's selection. 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. One of the options in each of the 3 selection boxes is "All", as in do not apply the filter.

Using filter

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

Using where

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

Using expression with where

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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