To get an objects attribute value corresponding to an InstrumentedAttribute
it should be enough to just get the key of the attribute from it's ColumnProperty
and fetch it from the object:
t.colname == getattr(t, Table.colname.property.key)
If you have a Column
it can get a bit more complicated because the property that corresponds to the Column
might have a different key. There currently doesn't seem to be a public API to get from a column to the corresponding property on a mapper. But if you don't need to cover all cases, just fetch the attr using Column.key
.
To support descending orderings you'll either need to construct the desc()
inside the function or poke a bit at non-public API's. The class of the descending modifier ClauseElement
is sqlalchemy.sql.expression._UnaryExpression
. To see if it is descending you'll need to check if the .modifier
attribute is sqlalchemy.sql.operators.desc_op
. If that case you can get at the column inside it via the .element
attribute. But as you can see it is a private class, so watch for any changes in that area when upgrading versions.
Checking for descending still doesn't cover all the cases. Fully general support for arbitrary orderings needs to be able to rewrite full SQL expression trees replacing references to a table with corresponding values from an object. Unfortunately this isn't possible with public API's at this moment. The traversal and rewriting part is easy with sqlalchemy.sql.visitors.ReplacingCloningVisitor
, the complex part is figuring out which column maps to which attribute given inheritance hierarchies, mappings to joins, aliases and probably some more parts that escape me for now. I'll give a shot at implementing this visitor, maybe I can come up with something robust enough to be worthy of integrating into SQLAlchemy.