user12923398
user12923398

Reputation:

Odoo 13 TypeError: strptime() argument 1 must be str, not datetime.dateE

class BirthDateAge(models.Model):

_inherit = "hr.employee"

birthday = fields.Date(string="DOB")

age = fields.Integer(string="Age")

@api.onchange('birthday')

def _onchange_birth_date(self):

    """Updates age field when birth_date is changed"""

    if self.birthday:

        d1 = datetime.strptime(self.birthday, "%Y-%m-%d").date()

        d2 = date.today()

        self.age = relativedelta(d2, d1).years

Error:


Odoo Server Error

Traceback (most recent call last):

File "/odoo/odoo-server/odoo/http.py", line 619, in _handle_exception

return super(JsonRequest, self)._handle_exception(exception)

File "/odoo/odoo-server/odoo/http.py", line 309, in _handle_exception

raise pycompat.reraise(type(exception), exception, sys.exc_info()[2])

File "/odoo/odoo-server/odoo/tools/pycompat.py", line 14, in reraise

raise value

File "/odoo/odoo-server/odoo/http.py", line 664, in dispatch

result = self._call_function(**self.params)

File "/odoo/odoo-server/odoo/http.py", line 345, in _call_function

return checked_call(self.db, *args, **kwargs)

File "/odoo/odoo-server/odoo/service/model.py", line 93, in wrapper

return f(dbname, *args, **kwargs)

File "/odoo/odoo-server/odoo/http.py", line 338, in checked_call

result = self.endpoint(*a, **kw)

File "/odoo/odoo-server/odoo/http.py", line 910, in __call__

return self.method(*args, **kw)

File "/odoo/odoo-server/odoo/http.py", line 510, in response_wrap

response = f(*args, **kw)

File "/odoo/odoo-server/addons/web/controllers/main.py", line 1320, in call_kw

return self._call_kw(model, method, args, kwargs)

File "/odoo/odoo-server/addons/web/controllers/main.py", line 1312, in _call_kw

return call_kw(request.env[model], method, args, kwargs)

File "/odoo/odoo-server/odoo/api.py", line 387, in call_kw

result = _call_kw_multi(method, model, args, kwargs)

File "/odoo/odoo-server/odoo/api.py", line 374, in _call_kw_multi

result = method(recs, *args, **kwargs)

File "/odoo/odoo-server/odoo/models.py", line 6070, in onchange

record._onchange_eval(name, field_onchange[name], result)

File "/odoo/odoo-server/odoo/models.py", line 5871, in _onchange_eval

method_res = method(self)

File "/odoo/odoo-server/addons/hr/models/hr_employee.py", line 87, in _onchange_birth_date

('married', 'Married'),

TypeError: strptime() argument 1 must be str, not datetime.dateE

Upvotes: 0

Views: 1427

Answers (4)

Kenly
Kenly

Reputation: 26678

self.birthday is already of type Date, so you do not need to use the DateTime module to convert it.

You can reduce you code to one line:

self.age = relativedelta(fields.Date.today(), self.birthday).years

Upvotes: 0

user12923398
user12923398

Reputation:

d1 = datetime.strptime(str(self.birthday), "%Y-%m-%d").date()

Upvotes: 0

m0r7y
m0r7y

Reputation: 720

Since Odoo 11, fields of type date and datetime are object of type date or datetime, not string anymore.
So you don't have to use this :

d1 = datetime.strptime(self.birthday, "%Y-%m-%d").date()

just use this instead :
d1 = self.birthday

Upvotes: 1

May.D
May.D

Reputation: 1910

Have a look at Python datetime module documentation here.

As your error suggest, the first argument of the strptime method should be the format string. So just revert your parameters like below.

d1 = datetime.strptime("%Y-%m-%d", self.birthday).date()

Upvotes: 0

Related Questions