In Openerp, we have object_A with one one2many field belonging to object_B. Object_B has a float field. In object_A we have a one2many_list widget for the corresponding object_B so naturally we'd have multiple rows for each new record.
I know it's trivial but I'm having a hard time writing a function in object_A to sum up the total value of Object_B float column. What i have so far is something like that:
def get_result(self, cr, uid, ids):
total = {}
for obj in self.browse(cr, uid, ids):
result=0.0
total[result]+= obj.o2m_field.float_field
return total
The code provided by @DReispt should work, and if you approve an answer, please approve his, and not mine.
The important thing to understand is that a function field in OpenERP returns a dictionary with the object ids for the key and the value of the field for the given object as the associated value.
In your orginal code:
result = 0.0
total[result] += anything
would result in a KeyError
since the dictionary is empty originally (total = {}
at the beginning of your code).
A shorter version of DReispt code would be
def get_result(self, cr, uid, ids, context=None):
total = {}
for obj in self.browse(cr, uid, ids, context=context):
total[obj.id] = sum(o2m.float_field for o2m in obj.o2m_field)
return total
This version takes advantage of Python's generator expression which can be passed to the sum()
built in function. It is also slightly faster, because you avoid accessing the total
dictionary multiple times for each object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With