0
votes

I am using the datetime field for SMS server. I want the SMS to be sent always on the current date, but I want the user to be able to change the only the time from a view.

I am using a datetime field that will be used to post a request to the sms server, and a float field for the time, which is the one that is going appear on the view. What I want is to take the values from the view for the float field for the hour and minutes and pass it to the datetime field to set time. i.e I want to replace the hard coded values for hour and minute parameters inside the replace method, with values from the delivery_time field.

import datetime as dt

class SmsTemplate(models.Model):

    _name = "sms.template"
    delivery_time = fields.Float(string='Time')
    delivery_datetime = fields.Datetime(default=timezone('Asia/Baghdad').localize(dt.datetime.today()).replace(hour=1,minute=20))


<field name="delivery_time" widget="float_time"/>

1

1 Answers

0
votes

You almost had it. In this case, add a method depending on the delivery_time field, that updates the delivery_datetime field when changed.

@api.multi
@api.onchange('delivery_time')
def _update_datetime(self):
    for template in self:
        template.delivery_datetime = template.delivery_datetime + datetime.timedelta(hours=template.delivery_time)

The code above should help you on your way.