简体   繁体   中英

How can I set the time only from a view for a datetime field in odoo?

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. ie 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"/>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM