How to pass value with onchange one2many variable in odoo?

爱⌒轻易说出口 提交于 2019-12-24 08:47:08

问题


                    <field name="salary_month"/>
                    <field name="earning_type_id">
                        <tree editable="bottom">
                            <field name="earnings_type" />
                            <field name="based_on"  on_change="calc_amount(based_on,salary_month)" />
                            <field name="amount" />
                            <field name="total" />
                        </tree>
                    </field>

In the above condition I have two variables one is salary_month another one is one2many variable earning_type_id. While on change inside earning_type_id I need to pass the value of salary_month. Its shows undefined variable salary_month.

class employee_payroll_earnings(models.Model):    
    _name = 'employee.payroll.earnings' 

    earnings_id=fields.Integer()
    earnings_type=fields.Many2one('earnings.type','Earnings')
    based_on=fields.Selection([('fixed','fixed'),('percentage','percentage')], 'Based On')
    amount=fields.Float('Amount or Percentage')
    total=fields.Float('Total')

    @api.multi
    def calc_amount(self,based_on,salary_month):
        print based_on
        print salary_month

class hr_employee(models.Model):
    _inherit = 'hr.employee'     
    salary_month = fields.Float('Salary/Month', required=True)
    earning_type_id = fields.One2many('employee.payroll.earnings','earnings_id','Earnings')

回答1:


No need to write onchange attribute in view file. With new API, we can directly achieve onchange functionality with @api.onchange('field_name')

We can pass context in one2many field and get that value with self._context.get('context_key_name')

Try with following:

<field name="salary_month"/>
<field name="earning_type_id" context="{'salary_month': salary_month}">
    <tree editable="bottom">
        <field name="earnings_type" />
        <field name="based_on"  on_change="calc_amount(based_on)" />
        <field name="amount" />
        <field name="total" />
    </tree>
</field>


@api.onchange('based_on')
def onchange_calc_amount(self):

    context = self._context

    if context.has_key('salary_month'):

        print self.based_on
        print context.get('salary_month')


来源:https://stackoverflow.com/questions/41842073/how-to-pass-value-with-onchange-one2many-variable-in-odoo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!