This works but would be very inefficient
grand_total=sum(db.product(id).promotion_price*qty else:
db.product(id).unit_price*qty for id, qty in session.order.items() if
db.product(id).promotion_price)
(one query per item)
*Instead*
grand_total=sum(item.promotion_price*session.order[item.id] for item in
*db(db.product.id.belongs(*session.order.keys())&(db.product.promotion_price>0)).select())
(one single query)
On Sunday, 28 April 2013 01:27:22 UTC-5, 黄祥 wrote:
>
> hi folks,
>
> i want to do conditional sum() row table in controller, but when i try to
> do, it returns an error :
> Traceback
>
> 1.
> 2.
> 3.
> 4.
> 5.
> 6.
> 7.
> 8.
> 9.
> 10.
>
> Traceback (most recent call last):
> File "/host/Download/web2py/gluon/restricted.py", line 211, in restricted
> ccode = compile2(code, layer)
> File "/host/Download/web2py/gluon/restricted.py", line 194, in compile2
> return compile(code.rstrip().replace('\r\n', '\n') + '\n', layer, 'exec')
> File
> "/host/Download/web2py/applications/onlineshopping/controllers/default.py"
> <http://127.0.0.1:8000/admin/default/edit/onlineshopping/controllers/default.py>,
> line 181
> grand_total=sum(if db.product(id).promotion_price:
> db.product(id).promotion_price*qty else: db.product(id).unit_price*qty for
> id, qty in session.order.items())
> ^
> SyntaxError: invalid syntax
>
>
> any ideas how to achieve it?
> thanks and best regards
>
--
---
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 [email protected].
For more options, visit https://groups.google.com/groups/opt_out.