python - Can I do a dynamic comparison on SQLAlchemy? -


i have model:

class puc(base):    pucid = column(integer, primary_key=true)    asset = column(tinyint)    article = column(tinyint)    more values ...    more values ... 

and need query dynamically (this way tried):

pucs = session.query(puc).filter(puc[unique_by_param] == 1).all() 

the value of unique_by_param come frontend. example of unique_by_param is: {str}'asset', {str}'article', {str}'another_model_value'

what need way do. session.query(puc).filter(puc.asset == 1) or session.query(puc).filter(puc.article == 1) dynamically, first way tried.

the result using (puc[unique_by_param]) typeerror: 'declarativemeta' object not subscriptable

there way have used before, isn't pretty way that, isn't pretty way that:

# accounting table, have around 250 columns  #and special columns around 70 variables...  #so isn't option o this. if unique_by_param == 'asset':     q = (puc.asset == 1) elif unique_by_param == 'article':     q = (puc.article) elif ...more values:  pucs = session.query(puc).filter(or_(*q)) 

here's approach uses filter_by , keyword argument unpacking:

keyword = {unique_by_param : 1} session.query(puc).filter_by(**keyword).all() 

Comments