[web2py] Re: Creating a sums value from a column on an SQLFORM.grid

2022-04-08 Thread Jim S
I did a bunch of work in py4web yesterday getting this to work.  I was 
getting a similar error - I updated the py4web 'grid' code to fix it.  I've 
never used an Expression like this in web2py before so I can't say for sure 
that it is supported.

A couple other things I noticed.
1. You don't have a groupby specified on your .grid call.  With your code, 
if you get past your error, will only return 1 row with the total.
2. You're asking for the payments.amount field to be displayed as well as 
the payments_sum_field. It doesn't make sense to me that you'd want to 
display the individual amount along with the sum. I think you want 
something more like this:




*def payments():payments_sum_field = 
db.payments.amount.sum().with_alias('total')
grid_table=SQLFORM.grid(db.payments, fields=[db.payments.client, 
db.payments.recorded_on, payments_sum_field], groupby=[db.payments.client, 
db.payments.recorded_on])return locals()*

With this you'll get one 1 record for each client for each day with their 
total payments received.

But, you'll still have to find how to fix your Expression object has no 
attribute tablename issue.

-Jim

On Friday, April 8, 2022 at 1:09:57 AM UTC-5 mostwanted wrote:

> I'm trying this code below & i'm getting * 'exceptions.AttributeError'> 'Expression' object has no attribute 
> 'tablename' *   ERROR*, *whats wrong with my code? or What am i missing??
>
>
>
>
> *def payments():payments_sum_field = 
> db.payments.amount.sum().with_alias('total')
> grid_table=SQLFORM.grid(db.payments, fields=[db.payments.client, 
> db.payments.amount, db.payments.recorded,db.payments.recorded_on, 
> payments_sum_field])return locals()*
>
> On Thursday, April 7, 2022 at 2:26:48 PM UTC+2 Jim S wrote:
>
>> It looks like there is support for groupby in SQLFORM.grid which is what 
>> you'd need to build the proper SQL statement.
>>
>> I haven't tried this with web2py.  If I were, I'd try adding a groupby 
>> clause and a list of fields that include your sum field.  Something like 
>> this (not tested)
>>
>> payments_sum_field = db.payments.amount.sum()
>> SQLFORM.grid(db.payments, fields=[db.payments.field_1, 
>> db.payments.field_2, payments_sum_field], groupby=[db.payments.field_1, 
>> db.payments.field_2])
>>
>>
>> FWIW - I just submitted a PR for py4web to do exactly what you're talking 
>> about.  Looking at the code in web2py for groupby I see that I can improve 
>> what I'd submitted so far.
>>
>> -Jim
>>
>> On Thursday, April 7, 2022 at 3:29:37 AM UTC-5 mostwanted wrote:
>>
>>> Hi guys, I was wondering if i could create a sums column on an 
>>> SQLFORM.grid so that every search that's made produces a sums value, 
>>> something like this:
>>>
>>>
>>>
>>>
>>>
>>>
>>> *def payments():grid = SQLFORM.grid(db.payments, 
>>> args=[db.payments])if grid.process().accepted:query=("#Search 
>>> queries of the SQLFORMgrid")amount = 
>>> db(query).select((db.payments.amount).sum().with_alias('total'))return 
>>> locals()*
>>>
>>> Whats possible and whats not?
>>>
>>> Regards;
>>>
>>>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/web2py/463579a0-58b7-42b0-9cff-97645d10cb40n%40googlegroups.com.


[web2py] Re: Creating a sums value from a column on an SQLFORM.grid

2022-04-08 Thread mostwanted
I'm trying this code below & i'm getting * 'Expression' object has no attribute 
'tablename' *   ERROR*, *whats wrong with my code? or What am i missing??




*def payments():payments_sum_field = 
db.payments.amount.sum().with_alias('total')
grid_table=SQLFORM.grid(db.payments, fields=[db.payments.client, 
db.payments.amount, db.payments.recorded,db.payments.recorded_on, 
payments_sum_field])return locals()*

On Thursday, April 7, 2022 at 2:26:48 PM UTC+2 Jim S wrote:

> It looks like there is support for groupby in SQLFORM.grid which is what 
> you'd need to build the proper SQL statement.
>
> I haven't tried this with web2py.  If I were, I'd try adding a groupby 
> clause and a list of fields that include your sum field.  Something like 
> this (not tested)
>
> payments_sum_field = db.payments.amount.sum()
> SQLFORM.grid(db.payments, fields=[db.payments.field_1, 
> db.payments.field_2, payments_sum_field], groupby=[db.payments.field_1, 
> db.payments.field_2])
>
>
> FWIW - I just submitted a PR for py4web to do exactly what you're talking 
> about.  Looking at the code in web2py for groupby I see that I can improve 
> what I'd submitted so far.
>
> -Jim
>
> On Thursday, April 7, 2022 at 3:29:37 AM UTC-5 mostwanted wrote:
>
>> Hi guys, I was wondering if i could create a sums column on an 
>> SQLFORM.grid so that every search that's made produces a sums value, 
>> something like this:
>>
>>
>>
>>
>>
>>
>> *def payments():grid = SQLFORM.grid(db.payments, args=[db.payments])  
>>   if grid.process().accepted:query=("#Search queries of the 
>> SQLFORMgrid")amount = 
>> db(query).select((db.payments.amount).sum().with_alias('total'))return 
>> locals()*
>>
>> Whats possible and whats not?
>>
>> Regards;
>>
>>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/web2py/1e60c2a0-c3d8-4109-96de-51236f41cc26n%40googlegroups.com.