Hi,
I have a function that works well and helps to compute the sum of fields 
referenced by a list:reference field in another table :

table 'line' with field:
     'price','double'
table 'invoice' with fields:
    'line_id', 'list:reference'
    'total','double'

def sum_total(row):
t=0
for id_line in row.line_id: t=t+db.line[id_line].price
return t;

db.invoice.total.compute=lambda r:sum_total(r)

This works well but I have to define this function for a lot of different 
'total'-like fields. For the sake of learning, I'd like to write a function 
that could pass the 'line' field's name too. Something like :

table 'line' with field:
     'price_a','double'
     'price_b','double'
     ...

table 'invoice' with fields:
    'line_id', 'list:reference'
    'total_a','double'
    'total_b','double'
    ...

def sum_total(line_field_name,row):
t=0
for id_line in row.line_id: t=t+db.line[id_line].fieldname[line_field_name]
return t;

db.invoice.total_a.compute=lambda r:sum_total('price_a',r)
db.invoice.total_b.compute=lambda r:sum_total('price_b',r)
...

That make not the model crash, but it does not return an error either, and 
does not compute anything : on update field is not updated, on create 
field's value is 0.
I think it's because db.table.fieldname is a set and that I cannot pick one 
by the record's value. How can I pick one by its name ?

-- 
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.
For more options, visit https://groups.google.com/d/optout.

Reply via email to