Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask-SQLAlchemy filters and operators

Flask-SQLAlchemy gives the option to filter a query. There are a wide number of ways you can filter a query - the examples the Flask-SQLAlchemy docs give:

User.query.filter_by(username='peter') # Returns all users named 'peter'
User.query.filter(User.email.endswith('@example.com')) # Returns all users with emails ending in '@example.com'

I also found this for one-to-many relationships:

User.query.filter(User.addresses.any(address=address)) # Returns all users who have a particular address listed as one of their addresses

Questions:

  • Does anyone know what filters are actually available to be used? I can't find a list of filters anywhere in the documentation, which makes it rather hard to query databases.
  • In particular, what filter would I use to check if a user's email is contained within a particular set of email addresses?
like image 319
Matthew Avatar asked Feb 10 '14 10:02

Matthew


1 Answers

For a list of filters check SQLAlchemy documentation

what filter would I use to check if a user's email is contained within a particular set of email addresses?

Columns have a .in_() method to use in query. So something like:

res = User.query.filter(User.email.in_(('[email protected]', '[email protected]')))

Here you can find the list of column method for expressions.

like image 92
Paolo Casciello Avatar answered Oct 19 '22 05:10

Paolo Casciello