如何解决错误“ ValueError:时间数据'2019-07-01 07:00:00'与格式'%y-%m-%d%H:%M:%S'不匹配”

时间:2019-07-05 15:56:54

标签: python-3.x odoo-11

当我尝试执行通过修改请求修改hr.attendance字段的批准操作时,此错误显示。我尝试在网上搜索此错误,但主要的答案是数据库的格式不相同与代码,但在我的情况下,我看到了数据库的数据,但发现使用了相同的格式。关于如何解决此问题的任何想法吗?

小时出勤

enter image description here

modification.request

enter image description here

这是我的代码

 @api.multi
def modification_approval(self):
    attend_signin_ids = self.env['hr.attendance'].search([('employee_id','=',self.employee_id.id)])
    check_in_date = datetime.datetime.strptime(self.time_check_in_1, "%y-%m-%d %H:%M:%S").date()
    check_out_date = datetime.datetime.strptime(self.time_check_out_1, "%y-%m-%d %H:%M:%S").date()
    for obj in attend_signin_ids:
        attendance_check_in_date = datetime.datetime.strptime(obj.check_in, "%y-%m-%d %H:%M:%S").date()
        attendance_check_out_date = datetime.datetime.strptime(obj.check_out, "%y-%m-%d %H:%M:%S").date()
        if (check_in_date == attendance_check_in_date):
            obj.write({'check_in': self.time_check_in_1,
                       'check_out': self.time_check_out_1})

    return self.write({
        'state': 'approved'
    })

跟踪

Traceback (most recent call last):
File "/opt/openhrms/odoo/http.py", line 651, in _handle_exception
return super(JsonRequest, self)._handle_exception(exception)
File "/opt/openhrms/odoo/http.py", line 310, in _handle_exception
raise pycompat.reraise(type(exception), exception, sys.exc_info()[2])
File "/opt/openhrms/odoo/tools/pycompat.py", line 87, in reraise
raise value
File "/opt/openhrms/odoo/http.py", line 693, in dispatch
result = self._call_function(**self.params)
File "/opt/openhrms/odoo/http.py", line 342, in _call_function
return checked_call(self.db, *args, **kwargs)
File "/opt/openhrms/odoo/service/model.py", line 97, in wrapper
return f(dbname, *args, **kwargs)
File "/opt/openhrms/odoo/http.py", line 335, in checked_call
result = self.endpoint(*a, **kw)
File "/opt/openhrms/odoo/http.py", line 937, in __call__
return self.method(*args, **kw)
File "/opt/openhrms/odoo/http.py", line 515, in response_wrap
response = f(*args, **kw)
File "/opt/openhrms/addons/web/controllers/main.py", line 938, in call_button
action = self._call_kw(model, method, args, {})
File "/opt/openhrms/addons/web/controllers/main.py", line 926, in _call_kw
return call_kw(request.env[model], method, args, kwargs)
File "/opt/openhrms/odoo/api.py", line 689, in call_kw
return call_kw_multi(method, model, args, kwargs)
File "/opt/openhrms/odoo/api.py", line 680, in call_kw_multi
result = method(recs, *args, **kwargs)
File "/opt/openhrms/addons/sifast_attendance_modification_request/models/hr_attendance_modification_request.py", line 43, in modification_approval
check_in_date = datetime.datetime.strptime(self.time_check_in_1, "%y-%m-%d %H:%M:%S").date()
File "/usr/lib/python3.6/_strptime.py", line 565, in _strptime_datetime
tt, fraction = _strptime(data_string, format)
File "/usr/lib/python3.6/_strptime.py", line 362, in _strptime
(data_string, format))
ValueError: time data '2019-07-01 07:00:00' does not match format '%y-%m-%d %H:%M:%S'

1 个答案:

答案 0 :(得分:4)

%y-%m-%d %H:%M:%S更改为%Y-%m-%d %H:%M:%S,那么只有您可以使用它strptime方法,因为您的时间数据的格式为2019-07-01

%y - year without a century (range 00 to 99)
%Y - year including the century

请参考pyhton的strptime方法中使用的link格式