تخطي للذهاب إلى المحتوى
القائمة
لقد تم الإبلاغ عن هذا السؤال
3 الردود
26773 أدوات العرض

I used a float field with widget "float_time" to display a time. How I can convert datetime to float to display correctly in the widget?.

I explain a little more my scenario,

I have a field field 'start_date' which is datetime, I want that in the event "on_change" I set the time value in another field of type float that has the widget "float_time".

 'start_date': fields.datetime('Fecha Inicio' , required = True),
 'h_inicio': fields.float('Hora inicio', help="Hora inicio" , required = True),

In view :

<field name="start_date" on_change="onchange_f_inicio(start_date)"/>
<field name="h_inicio" widget="float_time"/>

Method on_change:

    def onchange_f_inicio(self, cr, uid, ids ,fecha_inicio):
    res = {'value':{}}
    fecha_ini = datetime.strptime(fecha_inicio, "%Y-%m-%d %H:%M:%S")

   user = self.pool.get('res.users').browse(cr, uid, uid)
   tz = pytz.timezone(user.tz) if user.tz else pytz.utc
   fecha_ini = pytz.utc.localize(fecha_ini).astimezone(tz)     # convert start in user's timezone           

    hora_inicio = "%s.%s" % (str(fecha_ini.hour) , str(fecha_ini.minute))       
    res['value']['h_inicio'] =  round(float(hora_inicio),1)
    return res

The only way that has worked is to convert the date 'start_date' in a string and then convert to a float to be the value returned by the method on_change.

This form does not think it's the best, if I can help create a better way to thank you.

thanks

الصورة الرمزية
إهمال
أفضل إجابة

First, import the needed classes:

from datetime import time, timedelta

Then, on your method:

float_field_time = time.min + timedelta(seconds=self.float_field * 60 * 60)
الصورة الرمزية
إهمال
أفضل إجابة

You can call the util function in the client code float_time_convert()

الصورة الرمزية
إهمال

And where's that function?

المنشورات ذات الصلة الردود أدوات العرض النشاط
3
أغسطس 22
21044
1
يونيو 21
4254
1
مايو 24
3479
2
مارس 24
2419
2
نوفمبر 22
3473