Вопрос

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?
Это было полезно?

Решение

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_(('x1@dom1.com', 'x2@dom2.com')))

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top