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

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

1 回答 1

3

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

我们可以在one2many字段中传递上下文并使用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')
于 2017-01-25T05:55:48.050 回答