How to solve singleton error while looping in odoo?

71 Views Asked by At

Kindly find the code given below which raises singleton error while executing and please let me know how to loop to select a particular record. Any help is appreciatable!

Code:

@api.model
    def create(self, vals):
        curr = datetime.now()
        new_date = datetime.strftime(curr, '%Y-%m-%d')
        cal_obj = self.env['daily.attendance'].search([])
        if cal_obj:
            for co in cal_obj:
                co.date
            if co.date == new_date:
                raise ValidationError(_('''Current Date Attendance Already Exist!'''))
        return super(DailyAttendance, self).create(vals)

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

There are 1 best solutions below

2
Sakthi Priya On

To avoid singleton error, kindly add loop for self as given below:

@api.onchange('user_id')
def onchange_department(self):
    for rec in self:
        if rec.user_id == True:
            emps = rec.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