ValueError: Expected singleton: daily.attendance.line(123,124,125

ValueError: Expected singleton: daily.attendance.line(123,124,125

@api.model
def create(self, vals):
    curr = datetime.now()
    new_date = datetime.strftime(curr, '%Y-%m-%d')
    cal_obj = self.env['daily.attendance'].search([])

@api.constrains('date')
def _date_test_unique(self):
     for rec in self:
        if self.search_count([('date', '=', rec.date)]) > 1:
            raise ValidationError(_('Current Date Attendance Already Existed!'))
          
@api.onchange('user_id')
def onchange_department(self):
    if self.user_id == True:
        emps = self.env['hr.employee'].search([])
        emp_attd = []
        from datetime import datetime

        now = datetime.now() # current date and time
        check_in = now.strftime('%Y-%m-%d %H:%M:%S')
        check_in_from = now.strftime('%Y-%m-%d 05:30')
        check_out = now.strftime('%Y-%m-%d %H:%M:%S')
        check_out_from = now.strftime('%Y-%m-%d 14:30')
        for emp in emps:
            vals = {
                'employe_id':emp.id,
                'check_in': check_in_from,
                'check_out': check_out_from,
                'is_present': True
            }
            emp_attd.append([0, 0, vals])
        self.update({
            'employee_ids': emp_attd,
        })
    else:
        self.employee_ids = False
    return {
        'type': 'ir.actions.client',
        'tag': 'reload',
    }      

在if语句前写一个for循环

@api.onchange('is_present')
def onchange_attendance(self):
    for rec in self:
        if rec.is_present:
            rec.is_absent = False

当 Odoo 试图从记录集中获取 employee_ids 值但它需要一条记录时会发生错误。

for emp in self.employee_ids:

您需要遍历自身然后访问每个记录的 employee_ids 字段值:

示例:

def attendance_validate(self):
    for rec in self:
        for emp in rec.employee_ids:

您应该将以下代码移到 for 循环之外

self.write({'state': 'validate', })

示例:

hr_attendance = self.env['hr.attendance']
for rec in self:
    for emp in rec.employee_ids:
        if emp.is_present == True:
            attd_crete_id = hr_attendance .create({'employee_id': emp.employe_id.id,
                                         'check_in': emp.check_in,
                                         'check_out': emp.check_out,
                                         })
    rec.write({
        'state': 'validate',
    })
    ...

可能您需要调用写入状态来验证 attendance_validate 方法何时成功(最后)

改进:

  • 下面的表达式

      if emp.is_present == True:
    

    可以简化为:

      if emp.is_present:
    
  • 你正在使用两个字段is_presentis_absent,你可以简单地使用is_present并且当它的值为False(未设置)员工不在。

  • 你需要删除第二个没有用的if语句

    elif emp.is_absent == True:
        if emp.is_absent == True:
    
  • 避免在创建方法中引发验证错误,因为它会破坏工作流程,相反,您可以在 date 字段上定义一个 constraint

      @api.constrains('date')
      def _date_test_unique(self):
          for rec in self:
              if self.search_count([('date', '=', rec.date)]) > 1:
                  raise ValidationError(_('Current Date Attendance Already Existed!'))
    

更新:

创建方法应该return新创建的记录:

@api.model
def create(self, vals):
    res = super(CLASS_NAME, self).create(vals)
    # Your code
    return res