Odoo is a comprehensive suite of business applications, redesigned to be faster, more secure, and equipped with exciting AI solutions. It features a wide range of seamlessly integrated apps, making it a game-changer for businesses aiming to streamline operations and drive growth.
Odoo is known for its customizable fields and templates, making it adaptable for various business needs. However, developers might need to define new field types when businesses get new equipment or have complex operations. These fields can be based on existing types or entirely new ones. Since Odoo can use all data types allowed by PostgreSQL, it's important to understand how these fields will function in different scenarios. For example, if you create a new field for storing time values, you need to specify how Odoo will display, cache, and export this field.
In this blog, we will talk about creating a custom field type to represent the specific data type.
Create a Python file to define the fields.
from odoo.fields import Field
class CustomField(Field):
"""Field class in order to get all the basic features of an odoo field"""
type = 'type_of_field'
column_type = ('type', 'type')
def convert_to_column(self, value, record, values=None, validate=True):
# Converting to column in order to save the data
return value
def convert_to_record(self, value, record):
# Converting to column record in order to handle the data
return value or None
def convert_to_read(self, value, record, use_name_get=True):
# Converts the data to a readable format
return value
def convert_to_export(self, value, record):
# Converting the data to Excel or csv compatible
if value or value == "":
return value
return ''
When defining a new field type, you need to import it separately because it won't be included in the built-in odoo.fields. Here's an example of how to use the new field type in your model.py file:
from odoo import fields, models
from .model_name import CustomField
class ProductTemplate(models.Model):
_inherit = 'product.template'
point = fields.CustomField(string="Field name")
Define the JavaScript functions for the Widget. Create a js file under the static/src/js folder:
odoo.define(module_name.custom_widget', function (require) {
"use strict";
const { Component, useState } = owl;
const fieldRegistry = require('web.field_registry');
class CustomWidget extends Component {
// Logic for custom widget
}
fieldRegistry.add('custom_widget', CustomWidget);
return CustomWidget;
});
Use this widget in Views,
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="product_form_inherit_module_name" model="ir.ui.view">
<field name="name">product.template.form.point</field>
<field name="model">product.template</field>
<field name="inherit_id" ref="sale.product_template_form">
<field name="categ_id" position="after">
<field name="CustomField" widget="'custom_widget'"/>
</field>
</field>
</record>
</odoo>
You can successfully add a new field type in Odoo using this code.
Creating a custom field type in Odoo 16 showcases the platform's flexibility and adaptability. This feature allows you to tailor the system to meet specific business needs, making it a more effective ERP tool. However, it requires a good understanding of the Odoo framework and Python programming. In conclusion, creating custom field types, though challenging, highlights Odoo's robust and versatile nature. It enables the customization of business processes for greater efficiency, making it a powerful tool for enhancing business operations.