Search Here

Color Field and Color Picker (color_picker) Widgets in Odoo

 In this article, you will learn how to add the color field in Odoo. To add color fields in Odoo we are going to use built-in beautiful widgets provided by Odoo. It's a new feature in Odoo 15.

Color Field and Color Picker (color_picker) Widgets in Odoo

How to Use and Create Color Fields in Odoo

To add color fields in Odoo there are two options, in this tutorial, I will show you how to add color fields by using Integer and Char fields along with color widgets.

Read More: How to Use Email, Phone and URL Widget in Odoo

How to Add Color Field using Integer Field and widget="color_picker"

integer_color_widget.py

color = fields.Integer('Color')

How to Set Default Color in Color Field in Odoo

You can also set the default value to a color field like below.

Read More: How to Show Rainbow Man Effect in Odoo

def _default_color(self):

return randint(1, 11)

color = fields.Integer('Color')

Don't forgot to import below library.

from random import randint

integer_color_widget.xml

<field name="color" widget="color_picker"/>

How to Add Color Field using Char Field and widget="color"

char_color_widget.py

color = fields.Char('Color')

char_color_widget.xml

<field name="color" widget="color"/>

In the above code we have used Integer Field with widget="color_picker" and Char field with widget="color", if you will use Integer Field with widget="color" and Char field with widget="color_picker" then it will not work properly.

Post a Comment

0 Comments