GioViz88
GioViz88

Reputation: 17

How to update a field when I'm writing a value in other field in OpenERP 7?

I'm a newbie with openerp and I'm trying how can I need to know how can i make a code that calculates the result in a field when I write a value in other field, example:

field1 = 5000 

field2 = field1 * 5

I've read the docs and tried whit programming functions but always get an error.

Upvotes: 0

Views: 1529

Answers (2)

GioViz88
GioViz88

Reputation: 17

Finally I solved it. I used this code:

def _get_salario_diario(self, cr, uid, ids, field_name, arg, context=None):
    res= {}
    for record in self.browse(cr, uid, ids, context=context):
        res[record.id]= record.month_wage / 30
    return res
_columns = {
    'month_wage': fields.float('Salario Mensual Bs.', digits=(16,2)),
    'diary_wage': fields.function(_get_salario_diario, method=True, type='float', string='Salario Diario Bs.', store=True),
}

def onchange_month_wage(self, cr, uid, ids, month_wage, context=None):
        vals = {}
        if month_wage > 0:
            vals['diary_wage'] = salario_mensual / 30
        return {'value': vals}

and in my XML file:

<field name='month_wage' on_change="onchange_month_wage(month_wage)"/>
<field name='diary_wage'/>

It solved my problem and my module works rightly.

Upvotes: 0

Bhavesh Odedra
Bhavesh Odedra

Reputation: 11141

onchange is solution for your problem.

Here is example in .py side:

def onchange_field1(self, cr, uid, ids, field1, context=None):
    vals = {}
    if field1 > 0:
        vals['field12'] = field1 * 5
    return {'value': vals}

Here is example in .xml side:

<field name="field1" on_change="onchange_field1(field1)"/>

Upvotes: 0

Related Questions