跳至內容
選單
此問題已被標幟
1 回覆
4689 瀏覽次數

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
3903
6
9月 19
11179
2
3月 16
7817
1
1月 24
12650
0
3月 15
8106