Reputation: 4694
I have a Query object which I've done some filtering on. Now I pass that query around and then later I'd like to see what I've filtered on, using some kind of inspection.
I've got as far as:
>>> q = session.query(Activity).filter(Activity.label == "Foo")
>>> print(q.whereclause)
table.activity.label = :label_1
I can't find a way to get anything else out of that sqlalchemy.sql.elements.BinaryExpression
other than the column name (not the filtered value which I want, in this case "Foo").
Upvotes: 1
Views: 1546
Reputation: 4768
You can use a _criterion
attribute for more detailed info.
Examples:
In [1]: first = User.query.filter(User.confirmed_at.isnot(None))
In [2]: vars(first._criterion)
Out[2]:
{'_orig': (Column('confirmed_at', DateTime(), table=<users>),
<sqlalchemy.sql.elements.Null object at 0x7f7a667e25c0>),
'left': Column('confirmed_at', DateTime(), table=<users>),
'modifiers': {},
'negate': <function sqlalchemy.sql.operators.is_>,
'operator': <function sqlalchemy.sql.operators.isnot>,
'right': <sqlalchemy.sql.elements.Null object at 0x7f7a667e25c0>,
'type': NullType()}
In [3]: second = User.query.filter(User.login_count > 5)
In [4]: vars(second._criterion)
Out[4]:
{'_orig': (Column('login_count', Integer(), table=<users>, nullable=False, server_default=DefaultClause('0', for_update=False)),
BindParameter('%(140163529198168 login_count)s', 5, type_=Integer())),
'left': Column('login_count', Integer(), table=<users>, nullable=False, server_default=DefaultClause('0', for_update=False)),
'modifiers': {},
'negate': <function _operator.le>,
'operator': <function _operator.gt>,
'right': BindParameter('%(140163529198168 login_count)s', 5, type_=Integer()),
'type': Boolean()}
Upvotes: 3