You'd use a hybrid for this case, and due to the usage of float() you'd 
probably want to produce a separate @expression that doesn't rely on a Python 
function.

Docs and examples for hybrid are at  
http://www.sqlalchemy.org/docs/orm/extensions/hybrid.html

Separate @expression:

http://www.sqlalchemy.org/docs/orm/extensions/hybrid.html#defining-expression-behavior-distinct-from-attribute-behavior

The "float()" call in SQL would likely be using CAST, so take a look at 
http://www.sqlalchemy.org/docs/core/expression_api.html?highlight=cast#sqlalchemy.sql.expression.cast
 for that.



On Sep 7, 2011, at 2:27 PM, Tim Black wrote:

> What is the right way to use .order_by() to order by the values returned by a 
> model object property?  My model object is like this:
> 
> class Project(DeclarativeBase):
>     __tablename__ = 'project'
>     id = Column(Integer, primary_key=True)
>     ...
>     @property
>     def remainderDue(self):
>         return self.totalDue - float(self.totalPaid)
> 
> The query I'm trying to run is:
> 
> projects = 
> DBSession.query(model.Project).order_by(desc(model.Project.remainderDue))
> 
> This returns the following error:
> 
> Module sqlalchemy.sql.expression:1279 in _literal_as_text
> ArgumentError: SQL expression object or string expected.
> 
> Tim
> 
> 
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group at 
> http://groups.google.com/group/sqlalchemy?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to