Mostly right, except it's not a "query" object but an "expression" object. 
Note, that str(expression) returns the actual SQL statement that will be 
executed, so in theory it could be dependent on the particular db adapter 
being used. As you noticed, that SQL statement is used as the key for 
storing the result in the Row object (it is actually stored in an "_extra" 
dictionary within the Row object, but it can be accessed without explicit 
reference to _extra).

When you do:

monthly_amount_first[sum]

web2py translates that to:

monthly_amount_first[str(sum)]

which then retrieves:

monthly_amount_first['_extra'][str(sum)]

Anthony

On Thursday, December 1, 2011 7:49:00 AM UTC-5, Manakel wrote:
>
> Great, i believe it clarifies a lot of misunderstanding from my
> side :-).
>
> Basically, correct me if am wrong:
> - the query is only evaluated at the time of the "select".
> - we can create "query" object for both parts of the query and
> assemble them and they 'll be evaluated at the time of the "select"
> - when we create such "query" object in the select part, the result
> for each row can be accessed using the query object (because the query
> object is automatically evaluated to a string that is the key to the
> storage of the value in the row dictionnary)
> - there was no chance for any of my attempt to work,because it was too
> late (first() condition on a query already evaluated...)
>
> I start to love web2py :-)
>
> On 30 nov, 20:27, Carlos Hanson <[email protected]> wrote:
> > It is probably useful to see the following as well:
> >
> > >>> sum = db.operations.amount.sum().coalesce_zero()
> > >>> sum
> >
> > <gluon.dal.Expression object at 0x12e68d0>>>> print sum
> >
> > COALESCE(SUM(operations.amount),0)
> >
> > >>> monthly_amount_first = db((db.operations.date.year()==current_year) &
> >
> > ...                           (db.operations.date.month()==current_month)
> > ...                          ).select(sum).first()>>> 
> monthly_amount_first
> >
> > <Row {'_extra': <Row {'COALESCE(SUM(operations.amount),0)': 45.0}>}>>>> 
> monthly_amount_first[sum]
> >
> > 45.0
>
>

Reply via email to