I have a function field. The function of the field is
def _wage(self, cr, uid, ids, fieldnames, args, context=None):
res = {}
for obj in self.browse(cr, uid, ids, context=context):
s = (
"""select sum(wage) from hr_attendance where employee_id=%s and name >= date_trunc('month', current_date - interval '1' month)and name < date_trunc('month', current_date)""" % ( obj.employee_id.id))
cr.execute(s)
l = cr.fetchone()
value = l[0]
res[obj.id] = value
return res
This field calculate the wage using the query. Sometime I need to type and save the wage not based on the calculation. So I want that filed as editable.
I tried inverse function.
This the field
'wage': fields.function( string='Per Month Salary', fnct=_wage, fnct_inv=_set_wage, store=True, type='float'),
I give the inverse function as
def _set_wage(self, cr, uid, id, field_name, field_value, args=None, context=None):
obj = self.browse(cr, uid, id)
for record in obj:
if record.wage != field_value:
cr.execute(
'UPDATE hr_contract '
'SET wage=%s '
'WHERE id=%s', (field_value, id)
)
return True
When I give this function the field becomes editable but when I type and save the value it again changes to the computed value.
I also tried another method
def _set_wage(self, cr, uid, id, field_name, field_value, args=None, context=None):
obj = self.browse(cr, uid, id)
for record in obj:
if record.wage != field_value:
record.wage=self.write(cr, uid, id, {'wage': field_value})
return True
It shows error as
RuntimeError: maximum recursion depth exceeded in cmp
How can I store the value of the function field after it is edited?
Help me with an example.
Thanks...