How to use float_time widget in odoo

Hello Odooers & Developers!
Introduction

Welcome to our Odoo technical article, where I’ll be discussing how to use the float_time widget in Odoo. Odoo provides various widgets to enhance the usability and display of fields in views, and one such useful widget is float_time.

  • In this blog, you’ll learn:
    • What the float_time widget is and when to use it
    •  How to define a float field for time representation
    •  How to apply the float_time widget in an XML view
    •  A practical example in a custom module

By the end of this blog, you'll have a clear understanding of how to use float_time to display time values effectively in your Odoo forms.

What is the float_time Widget in Odoo?

The float_time widget is used to display time values in a more readable format in Odoo views. By default, Odoo stores time-related fields as float values representing hours. However, the float_time widget converts this float value into a human-readable time format (HH:MM).

For example:

  • A float value of 16.50 (hours) will be displayed as 16:30 (4:30 PM).
  • A float value of 9.75 will be displayed as 09:45 (9:45 AM).

This is particularly useful when working with business hours, shop closing times, employee shifts, or any feature involving time representation.

Defining a Float Field for Time in Python

Before using the float_time widget, you need to define a field of type Float in your Odoo model. Below is an example where we define a closing_time field for a shop in a Mall Management(custom) module.

Python Model (.py)

.py

from odoo import models, fields
class Mall(models.Model):
    _name = 'mall.shop'
    _description = 'Shop'
    closing_time = fields.Float(string='Closing Time', help='Time in hours')

Explanation:

  • We define a Float field named closing_time to store the shop’s closing time in hours.
  • The value will be stored as a decimal (e.g., 18.75 for 18:45 or 6:45 PM).

Using the float_time Widget in XML View

Now, let's apply the float_time widget in the form view to ensure that users see the time in a readable HH:MM format.

XML Form View (.xml)

.py

<record id="view_shop_form" model="ir.ui.view">
    <field name="name">view.shop.form</field>
    <field name="model">mall.shop</field>
    <field name="arch" type="xml">
    <form string="Shop">
    <sheet>
    <group>
    <group>
   <field name="name"/>
   <field name="floor"/>
   </group>
   <group>
   <field name="closing_time" widget="float_time"/>
   </group>
   </group>
   </sheet>
   </form>
   </field>
</record>

Explanation:

  • The <field name="closing_time" widget="float_time"/> applies the float_time widget.
  • This ensures that Odoo will convert the float value into HH:MM format in the form view.
  • Users can enter time in an easy-to-understand way rather than dealing with decimal values.

Conclusion

  • In this blog, we explored the float_time widget, which helps display float-based time values in a user-friendly HH:MM format.
  •  We covered:
    ✔ What float_time is and why it’s useful
    ✔ How to define a float field for time representation
    ✔ How to apply the float_time widget in an Odoo form view
    ✔ Practical implementation with a Custom Module example

By using the float_time widget, you can improve the usability of time-related fields in your Odoo modules.


If you want odoo technical training on any odoo version , please let us know by mail at [email protected]. Then next, our odoo expoert will conduct online or offline training with you 


Devintellecs & team are odoo training providers in the USA and INDIA, so we will try our best to give the training either individal or any bulk employee company.


if you want to check your odoo technical or functional knowledge, then we have prepared the odoo EXam practice test for the odoo technical & functional people.



Odoo DEV February 13, 2025
Share this post
Archive
Sign in to leave a comment
Understanding Related Field In Odoo