Skip to Content
Menu
This question has been flagged
2 Replies
2770 Views

am developing a module in odoo 10 that requires me to get time difference 

i have created 3 fields in my model of which the time difference field is to be filled automatically after computation

opened_field = fields.Datetime(string="DateTime Opened", required=False,)
closed_field = fields.Datetime(string="DateTime Closed", required=False,)
time_diff = fields.Float(string="Time Difference",  required=False )

and below is my function

@api.onchange('opened_field', 'closed_field','time_diff')
def calculate_date(self):
    if self.opened_field and self.closed_field:
        t1=datetime.strptime(str(self.opened_field),'%Y-%m-%d %H:%M:%S')
        t2=datetime.strptime(str(self.closed_field),'%%Y-%m-%d %H:%M:%S')
        t3=t2-t1
        self.time_diff=str(t3.days)

on running the code am getting this error 

Traceback (most recent call last):
  File "/home/rottal/Desktop/odoo-dev/odoo/odoo/http.py", line 642, in _handle_exception
    return super(JsonRequest, self)._handle_exception(exception)
  File "/home/rottal/Desktop/odoo-dev/odoo/odoo/http.py", line 684, in dispatch
    result = self._call_function(**self.params)
  File "/home/rottal/Desktop/odoo-dev/odoo/odoo/http.py", line 334, in _call_function
    return checked_call(self.db, *args, **kwargs)
  File "/home/rottal/Desktop/odoo-dev/odoo/odoo/service/model.py", line 101, in wrapper
    return f(dbname, *args, **kwargs)
  File "/home/rottal/Desktop/odoo-dev/odoo/odoo/http.py", line 327, in checked_call
    result = self.endpoint(*a, **kw)
  File "/home/rottal/Desktop/odoo-dev/odoo/odoo/http.py", line 942, in __call__
    return self.method(*args, **kw)
  File "/home/rottal/Desktop/odoo-dev/odoo/odoo/http.py", line 507, in response_wrap
    response = f(*args, **kw)
  File "/home/rottal/Desktop/odoo-dev/odoo/addons/web/controllers/main.py", line 892, in call_kw
    return self._call_kw(model, method, args, kwargs)
  File "/home/rottal/Desktop/odoo-dev/odoo/addons/web/controllers/main.py", line 884, in _call_kw
    return call_kw(request.env[model], method, args, kwargs)
  File "/home/rottal/Desktop/odoo-dev/odoo/odoo/api.py", line 689, in call_kw
    return call_kw_multi(method, model, args, kwargs)
  File "/home/rottal/Desktop/odoo-dev/odoo/odoo/api.py", line 680, in call_kw_multi
    result = method(recs, *args, **kwargs)
  File "/home/rottal/Desktop/odoo-dev/odoo/odoo/models.py", line 5518, in onchange
    record._onchange_eval(name, field_onchange[name], result)
  File "/home/rottal/Desktop/odoo-dev/odoo/odoo/models.py", line 5416, in _onchange_eval
    method_res = method(self)
  File "/home/rottal/Desktop/odoo-dev/todo_app/new_connections/models/connection.py", line 68, in calculate_date
    t1=datetime.strptime(str(self.opened_field),'%Y-%m-%d %H:%M:%S')
AttributeError: 'module' object has no attribute 'strptime'

 what might be the problem kindly assist

Avatar
Discard
Best Answer

Hi,

You have to import this,

from datetime import datetime


Also, there is an issue in the following line,

 t2=datetime.strptime(str(self.closed_field),'%%Y-%m-%d %H:%M:%S')

There is an extra %, remove it and try.


Thanks

Avatar
Discard
Author

Thank you it has worked for me importing the datetime and removing the extra % sign

hope this solution is still working in version 14. :)