If “use a function” means “write filters inside a function”, you can certainly - you just need to pass the correct argument (query) and use its return (filtered query) in the right place
Take an example from the SQLAlchemy tutorial:
wendy = session.query(User).filter_by(name='wendy').one()
You can easily move the filter to a function:
def my_filter(query): return query.filter_by(name='wendy') wendy = my_filter(session.query(User)).one()
However, if the function "that checks the object for some properties and returns boolean values depending on the result", you mean a function that takes a database record as an argument, I don’t think it can be done (without undermining the whole purpose of using SQL) .
- EDIT - . As doog abides points out, the hybrid extension, while not working with database records, does something that is equivalent for many practical purposes.
Of course, some people will insist on writing something like:
all_users = session.query(User).all() wendy= filter( lambda u:u.name=='wendy', all_users )
But, for the sake of common sense, your fellow programmers and users, do not do this.
source share