teggy
teggy

Reputation: 6335

sqlalchemy filter using in_

Is there a more efficient way to do the following? I am more interested in knowing if there is a way to set "mylist" to match anything if day is equal to 'all' because in other scenarios, "mylist" can contain a lot more elements.

if day == 'all':
    mylist = ['monday','tuesday','wednesday','thursday','friday','saturday','sunday']
else:
    mylist = [day] # day equal to one of the above

records = meta.Session.query(Transaction).filter(Transaction.day.in_(mylist)).all()

Upvotes: 0

Views: 5683

Answers (1)

David Z
David Z

Reputation: 131760

What about just not filtering on the day if it's equal to 'all'?

query = meta.session.query(Transaction)
if day != 'all':
    query = query.filter(Transaction.day == day)
records = query.all()

Upvotes: 7

Related Questions