Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you have a list of terms and want to find rows where a field matches one of them, then you could use the in_() method:</p> <pre><code>terms = ['term1', 'term2', 'term3'] query.filter(Cls.field.in_(terms)) </code></pre> <p>If you want to do something more complex, then <code>or_()</code> and <code>and_()</code> take <code>ClauseElement</code> objects as parameters. ClauseElement and it's subclasses basically represent the SQL <a href="http://en.wikipedia.org/wiki/Abstract_syntax_tree" rel="noreferrer">AST</a> of your query. Typically you create clause elements by invoking a comparison operator on Column or InstrumentedAttribute objects:</p> <pre><code># Create the clause element clause = (users_table.columns['name'] == "something") # you can also use the shorthand users_table.c.name # The clause is a binary expression ... print(type(clause)) # &lt;class 'sqlalchemy.sql.expression._BinaryExpression'&gt; # ... that compares a column for equality with a bound value. print(type(clause.left), clause.operator, type(clause.right)) # &lt;class 'sqlalchemy.schema.Column'&gt;, &lt;built-in function eq&gt;, # &lt;class 'sqlalchemy.sql.expression._BindParamClause'&gt; # str() compiles it to SQL print(str(clause)) # users.name = ? # You can also do that with ORM attributes clause = (User.name == "something") print(str(clause)) # users.name = ? </code></pre> <p>You can handle clause elements representing your conditions like any Python objects, put them into lists, compose them into other clause elements, etc. So you can do something like this:</p> <pre><code># Collect the separate conditions to a list conditions = [] for term in terms: conditions.append(User.name == term) # Combine them with or to a BooleanClauseList condition = or_(*conditions) # Can now use the clause element as a predicate in queries query = query.filter(condition) # or to view the SQL fragment print(str(condition)) # users.name = ? OR users.name = ? OR users.name = ? </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload