如何在odoo中通过onchange伪造字段值

时间:2016-10-04 04:19:27

标签: python-2.7 odoo-9

在我的课堂上有一个两个选择的现场出勤和休假类型,当出勤出现时,当我的出勤率是绝对的,假期类型选择字段是必需的,并且我选择作为病假,我选择休假类型当我编辑同一记录时我保存记录,并且我正在将出勤标记为现在,请假类型字段值应该为假。我只给出了假的字段,但是记录保存在数据库中...任何人都可以帮助这个

这是onchange功能

@api.onchange('attendance', 'employee_id', 'attendance_date')
def onchange_attendance(self):
    if self.attendance:
        if self.attendance == 'present':

            time_in = time_out = False

            self.present = True
            self.absent = False
            self.half_day = False
            self.late = False
            self.comp_off = False
            self.leave_type = False
            self.holiday = False
            self.weekly_off = False
            self.compensation_date = False
        if self.attendance == 'absent':
            self.present = False
            self.absent = True
            self.half_day = False
            self.late = False
            self.comp_off = False
            self.holiday = False
            self.weekly_off = False
            self.compensation_date = False
        if self.attendance == 'half_day':
            self.present = False
            self.absent = False
            self.half_day = True
            self.late = False
            self.comp_off = False
            self.holiday = False
            self.weekly_off = False
            self.compensation_date = False
        if self.attendance == 'comp_off':
            self.present = False
            self.absent = False
            self.half_day = False
            self.late = False
            self.comp_off = True
            self.leave_type = False
            self.holiday = False
            self.weekly_off = False
        if self.attendance == 'holiday':
            self.present = False
            self.absent = False
            self.half_day = False
            self.late = False
            self.comp_off = False
            self.leave_type = False
            self.holiday = True
            self.weekly_off = False
            self.compensation_date = False
        if self.attendance == 'weekly_off':
            self.present = False
            self.absent = False
            self.half_day = False
            self.late = False
            self.comp_off = False
            self.leave_type = False
            self.holiday = False
            self.weekly_off = True
            self.compensation_date = False

1 个答案:

答案 0 :(得分:0)

我总结了你的观点。

  1. 出勤=出现,然后离开Type = Readonly。
  2. 出勤=假,然后保留类型=必需。
  3. 您正在保存一个记录,其中Attendance = False,Leave Type = Sick Leave。(这是保存在DB中的值)。
  4. 现在您正在尝试编辑此记录。改变出勤率=真,现在离开类型=只读。因此,无法使用onchange方法更新此字段。
  5. 如果要更新此字段值,则必须在write方法中执行此操作。

    @api.multi
    def write(self, vals):
        res = super(*yourmodel*, self).write(vals)
        if vals.get('attendance') and vals.get('attendance') == 'present':
            self.leave_type = False
        return res
    

    我希望它会有所帮助。