Creating calculator fields in Odoo
Beginner
Since Odoo 13 there is a new option for fields to do mathematical formulas in fields. This new option has been introduced in Odoo 13 through the calculator widget and allows you to let end users do powerful computations in fields. In this tutorial you will learn how to create calculator fields and what options you have. We will inherit the existing product model and will add two calculator fields in this tutorial.
Before you can start creating calculator fields you don't need any dependency. As we'll add fields to the existing product model we'll need a dependency on the module 'product' though. Open up your manifest.py and add 'product' as a dependency:
'depends': ['product'],
Without this dependency we wouldn't be able to inherit the product model and do this tutorial. If you have a custom model you can just skip this step.
The first thing that we need to do is to inherit the product model. Create a new Python file named 'product_template.py' in the 'models' folder of your module. In this file we'll inherit the product model and we'll create two new fields. One integer field and one float field. Your code should look like this:
# -*- coding: utf-8 -*-from odoo import models, fields, apiclass ProductTemplate(models.Model): _inherit = 'product.template' """ Calculation fields are defined/created just like any other field in Odoo. The difference is in the view by adding widget='calculator'. """ calculation = fields.Float(string='Calculator field') calculation_int = fields.Integer(string='Calculator field (integer)')
Now that we have the fields created we should add them to the product view and we should let Odoo know that we want it to be calculator fields. As Odoo has two different product views, one for products and one for variants, we'll add the fields to both the views. Create a new file named 'product_view.xml' in the 'views' folder of your custom module. Now let's inherit the products view and add our fields after the barcode field:
Notice how we've added the widget="calculator" code. This will tell Odoo that this field should allow computations. Any end user can do formulas in these fields by starting with '=' and then the formula they want to do. For example: =4*3 will give back 12 if you click or tab outside of the field. Notice that the calculator widget also looks at what field you have. If your field is a float the computation will have decimal precision while an integer field will only give back rounded numbers. Now let us do the same for the product variants view:
That's it! If you now install your module and go to the product form you'll see the new fields showing up. Click in the custom field and fill in a formula, such as "=4*3" and you'll see it being computed by Odoo itself. The calculator field will only do computations if it starts with "=" though! If you'd fill in "4*3" nothing will happen. The "=" operator will let Odoo know that you really want to do computations in the field.
So which mathematical options do you have though? What is supported by default in this new widget? By default you can do the following computations:
The introduction of the calculator widget is a really great addition. Thanks to this being a widget (and not a custom field) you can quickly add the option to any existing or new field. This really opens a lot of options for end users to do quick calculations within Odoo without needing an external calculator. In the end this will speed up the end user its work and implementing it is very easy and fast. Changes are high that this new option will be showing up more and more in the upcoming Odoo versions.