Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

datetime timedelta datatype missmatch?

By
Lucas Huber
on 2/26/16, 11:31 AM 745 views

I try to add some minutes and hours to a Datetime field. Somehow I get a datatype missmatch and I can't figure out what is wrong. Everything that I try gets an error:

File "/opt/odoo/custom/....... in onchange_duration_hrs
end_time = (self.start_datetime + timedelta(hours=hour, minutes=minute))  
TypeError: coercing to Unicode: need string or buffer, datetime.timedelta found


File "/opt/odoo/custom/addons..... line 182, in onchange_duration_hrs
end_time = (self.start_datetime + datetime.timedelta(hours=hour, minutes=minute))
AttributeError: type object 'datetime.datetime' has no attribute 'timedelta'


If I just change the field self.start_datetime to datetime.now() the function works correct. 

start_datetime = fields.Datetime(string='Start DateTime')

end_time = (datetime.now() + datetime.timedelta(hours=hour, minutes=minute))

Any hint would be welcome?

 

0
Lucas Huber
On 3/2/16, 6:00 AM

Thanks to Yvans post I have now a solution (his code did cause still an error):

@api.multi  # getting integer out of float from widget "float_time" 
def _float_time_convert(self, float_val):
factor = float_val < 0 and -1 or 1
val = abs(float_val)
return (factor * int(math.floor(val)), int(round((val % 1) * 60)))


@api.one  # Getting the Stop DateTime
@api.onchange('start_datetime', 'toggle')
def _get_stop_time_hrs(self):
    if not (self.start_datetime and self.duration) or self.allday is True:
   return value
   start_time = datetime.strptime(self.start_datetime, DEFAULT_SERVER_DATETIME_FORMAT)
   hour, minute = self._float_time_convert(self.duration)
   end_time = (start_time + timedelta(hours=hour,   minutes=minute)).strftime(DEFAULT_SERVER_DATETIME_FORMAT)
    self.stop_datetime = end_time

But I still get an strftime error when I try to get the result back to datetime field with return value from method (without onchange api ofcourse)?

 return end_time




0
Yvan
On 2/26/16, 1:50 PM

Hi Lucas,

the problem is the type of self.start_datetime: it's a string and not as expected a datetime object. You have to convert the string to a datetime object before you add a timedelta. To to this, use the fields.Datetime.from_string method as shown below


end_time = fields.Datetime.from_string(self.start_datetime) + datetime.timedelta(hours=hours, minutes=minute)

Best regards

Yvan

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 2/26/16, 11:31 AM
Seen: 745 times
Last updated: 3/2/16, 6:00 AM