Ok, because the totalDue and totalPaid attributes are also SQLAlchemy
declarative model object properties, I converted them (and all other
similar property dependencies) to hybrid_properties and created the
associated @[property].expression methods for every hybrid_property
(though I think those @[property].expression methods are not needed
where SQLAlchemy is not converting a Python method like float() into an
SQL function--I'll test for that later). Now I get this error:
Module projects.model.main:189 in totalDue
>> return sum([w.totalDue for w in self.workDone]) +
cast(self.itemsPurchasedTotal, Float)
TypeError: 'InstrumentedAttribute' object is not iterable
By writing line 189 on several lines, it's apparent self.workDone causes
the error. self.workDone is a relation:
class Project(DeclarativeBase):
# ...
workDone = relation('WorkDone') # one-to-many
# ...
@totalDue.expression
def totalDue(self):
'''Allow this property to be accessed at the class level'''
return sum([w.totalDue for w in self.workDone]) +
cast(self.itemsPurchasedTotal, Float)
Do I need to convert that relation into a hybrid_property, or do
something else in order to use it in this order_by query? I'm beginning
to wonder if it's easier to deal with sorting by @properties by sorting
in Python after running the query--is that the case?
Tim
On 09/07/2011 03:19 PM, Michael Bayer wrote:
> 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.