如何在odoo中使用onchange one2many变量传递值? [英] How to pass value with onchange one2many variable in odoo?

查看:133
本文介绍了如何在odoo中使用onchange one2many变量传递值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

                    <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>

在上述条件下,我有两个变量,一个是salary_month,另一个是one2many变量earning_type_id.在earning_type_id内部进行更改时,我需要传递salary_month的值.它显示了未定义的变量salary_month.

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')

推荐答案

无需在视图文件中写入 onchange 属性.使用新的API,我们可以使用 @ api.onchange('field_name')

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

我们可以在 one2many 字段中传递上下文,并使用 self._context.get('context_key_name')

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

尝试以下操作:

<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')

这篇关于如何在odoo中使用onchange one2many变量传递值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆