New Time Field Reference in Odoo – Athiban Raj T

Real-time data synchronization – Kiruthika P
April 17, 2018
Integration Of To-Do With Odoo Calendar – Subhashree
April 18, 2018

New Time Field Reference in Odoo – Athiban Raj T

In this article, I’m going to walk you through the creation of a new time field in Odoo with the DateTime widget.

What is time field?

In Odoo, we have only a date-time and date field, not a time field for storing time values only. So I created one on Odoo8. All the values have to be stored in a database. In this case, I’m using a PostgreSQL database as it has the option to include the time format.

Field Declaration in Python file

In Open ERP, folder fields.py (openerp --> fields.py) is the file to declare fields in the custom add-ons folder. So, now we need to add a new class called ‘time’ with the time format.There’s another file called models.py (openerp --> models.py). This file contains the entire fields format and directly communicates with the PostgreSQL database.

The above changes are enough for new API users. If you’re following the old API, you have to create one more class under fields.py (opener p-->osv-->fileds.py) with the class name as time. Now the model will recognise our new field and we can key in our custom module python file like this:

Field View in XML File

Next, we’ll be creating the view for the new time field. All the views are under base.xml in the web folder and have many templates including field templates where we’ll be creating the new time template ..../.

Since this time field widget also depends on the Javascript, we need to enable access in view_form.js (openerp -> addons -> web -> static -> src -> js -> view_form.js) and create a new instance called ‘FieldTime’ which is an extension of the ‘FieldDatetime’ widget. The default ‘build_widget’ function has the date-time value in the current input field but here, I am including the time value alone as below:

instance.web.form.FieldTime = instance.web.form.FieldDatetime.extend({
template: "FieldTime",
build_widget: function() {
return new instance.web.TimeWidget(this);
}
});

Calendar widget mapping to the field

For selecting the time value, we have to provide a calendar widget in that field. I'm using the default calendar DateTime widget. In the ‘view_form.js’ file, I have created a new widget called TimeWidget which is an extension of the DateTime widget. In this widget, I keyed in ‘type_of_date’ as time.

instance.web.TimeWidget = instance.web.DateTimeWidget.extend({
jqueryui_object: 'datetimepicker',
type_of_date: "time"
});

Add Time Field in Search View

In the search box, if you select the time fields it should show ‘filter’ or ‘group by’. For that have to include our time field widget name in search.js (openerp -> addons -> web -> static -> src -> js -> search.js) file.

“instance.web.Registry” is a widget to register all field widgets which is where we need to include the TimeWidget. Then you can see the time field label in ‘advance filter’ drop down field.

This process can be followed wherever we need a time field. I have used it in the HR module for leaves & permissions to help me calculate the number of hours of permission taken by employees.

Keywords: Time field, Odoo, OpenERP, how to, time, date, format, module, ERP, python, PostgreSQL, add widget

In this article, I’m going to walk you through the creation of a new time field in Odoo with the DateTime widget.

Leave a Reply

Your email address will not be published. Required fields are marked *