Chapter 3 - Odoo 15 Development Book

Monetary fields

Monetary fields are mainly used for storing the amount in a certain currency. Odoo provides a dedicated feature for monetary values associated with a currency. All amount fields are monetary fields or float fields with monetary widget. Let’s check how to create a monetary field

First create a python file and add the following code

from odoo import api, fields, models
class FeeInherit(models.Model):
   _inherit = "hr.employee"
   company_id = fields.Many2one('res.company', store=True, copy=False,
                                string="Company",
                                default=lambda self: self.env.user.company_id.id)
   currency_id = fields.Many2one('res.currency', string="Currency",
                                 related='company_id.currency_id',
                                 default=lambda
                                 self: self.env.user.company_id.currency_id.id)
   fee = fields.Monetary(string="Fee")

In this code, we set the current company currency as the default currency. Next, add an XML file to shows the field on the user interface.

<form>
   <sheet>
       <group>
           <group>
               <field name="currency_id" invisible="1"/>
               <field name="fee"/>
           </group>
       </group>
   </sheet>
</form>

We can set the fee field as a float field and in the XML file give a widget for fee field as a monetary.

<field name="fee" widget="monetary"/>

We can see that on the user interface.

odoo-crm
whatsapp_icon
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message