I want to create query in sql alchemy filter, but column is(dynamic) in variable/specified in variable.
Original Query:
db_session.query(Notice).filter(Notice.subject.like("%" +query+ "%"))
I want to do query like this:
col_name='subject' db_session.query(Notice).filter(Notice.col_name.like("%" +query+ "%")) col_name='status' status=0 db_session.query(Notice).filter(Notice.col_name != 1)
Just use getattr standard python library function to get an attribute by name:
col_name = 'subject' db_session.query(Notice).filter(getattr(Notice, col_name).like("%" + query + "%"))
1.4m articles
1.4m replys
5 comments
56.9k users