跳至内容
菜单
此问题已终结
1 回复
4687 查看

In my odoo module I have a datetime field (prefered_date). What I want to do is that, no matter what time the user enters, the time is always set 10 am. I tried to do it with the following code. but is not working. The time is setting to 6 am instead of 10, Maybe it has something to do with timezone. What am I doing wrong?

@api.multi
def write(self, values):
    if 'prefered_date' in values:
        date = datetime.strptime(values.get('prefered_date'), '%Y-%m-%d %H:%M:%S')
        newdate = date.replace(hour=10, minute=0)
        new = newdate.strftime("%Y-%m-%d %H:%M:%S")
        values['prefered_date'] = new 
    return super(PostabilidadRequest, self).write(values)


形象
丢弃
最佳答案

Hello Ernesto Ruiz,

Find code in Comment. 

Hope it will help you. 

Thanks & Regards,
Email: odoo@aktivsoftware.com
Skype: kalpeshmaheshwari

形象
丢弃

You will need to convert the datetime to the timezone. Refer to the code:

def write(self, values):
if 'qa_deadline' in values:
date = datetime.strptime(values.get('qa_deadline'), '%Y-%m-%d %H:%M:%S')
user_date = datetime.combine(date.date(), time(10))
user_tz = pytz.timezone(self.env.context.get("tz") or self.enc.user.tz)
to_user = user_tz.localize(user_date)
tz_utc = pytz.timezone('UTC')
utc_time = to_user.astimezone(tz_utc)
utc_time = utc_time.replace(tzinfo=None)
values['qa_deadline'] = utc_time
return super(ProjectTask, self).write(values)

相关帖文 回复 查看 活动
3
7月 25
3902
6
9月 19
11179
2
3月 16
7816
1
1月 24
12649
0
3月 15
8105