Monetary Field and Widget Monetary in Odoo

Abid Patel
25-Oct-2024 Updated : 25-Oct-2024

Learn how to use the monetary field and widget in Odoo to manage and display currency values accurately, supporting localization and multi-currency.

Monetary Field and Widget Monetary in Odoo

In Odoo, managing currency-related fields requires specialized handling, especially when displaying prices, amounts, or other monetary values. The Monetary Field and widget="monetary" provide the perfect solution to format and display currency values effectively. These features offer localization and multi-currency support, enhancing the financial management capabilities in Odoo.

Understanding the Monetary Field in Odoo

A monetary field in Odoo is a type of field designed to hold currency values. It is similar to a float field but includes additional currency support to format values properly. This field works in conjunction with a currency field to show the appropriate currency symbol or format based on the selected currency.

How to Define a Monetary Field in Odoo

To create a monetary field in a model, specify the field type as fields.Monetary in Python and add a currency field to manage the currency symbol.

Example Code for a Monetary Field

Here is a code example to add a monetary field for prices:

python
<from odoo import models, fields>

class ProductTemplate(models.Model):
    _name = 'product.template'
    
    price = fields.Monetary(string="Price", currency_field="currency_id")
    currency_id = fields.Many2one('res.currency', string="Currency")

Explanation:

  • price: A monetary field that stores the product's price.
  • currency_id: A Many2one field referencing res.currency, ensuring the right currency symbol displays alongside the price.

Using the Monetary Widget in Odoo XML Views

To display monetary fields in Odoo’s views, use the widget="monetary" attribute in the XML view definition. This widget enables Odoo to properly format the amount based on the selected currency and locale.

Here is an example of using the monetary widget in a form view:

xml
<record id="view_product_template_form" model="ir.ui.view">
    <field name="name">product.template.form</field>
    <field name="model">product.template</field>
    <field name="arch" type="xml">
        <form string="Product">
            <field name="name"/>
            <field name="price" widget="monetary" options="{'currency_field': 'currency_id'}"/>
            <field name="currency_id"/>
        </form>
    </field>
</record>

Explanation:

  • price field with widget="monetary": This widget formats the price value with the correct currency symbol.
  • options="{'currency_field': 'currency_id'}": Specifies the currency field used to format the monetary value.

Benefits of Using the Monetary Widget

The monetary widget in Odoo offers various benefits:

  • Automatic currency formatting: Displays currency symbols automatically based on the currency selected.
  • Localization: Adjusts formatting based on the user’s locale settings.
  • Multi-currency support: Supports multiple currencies, making it ideal for international business management.

Use Cases for the Monetary Field in Odoo

Some common scenarios where the monetary field and widget are useful include:

  • ▹ Setting product prices in multi-currency environments.
  • ▹ Displaying invoice totals and other financial data with accurate currency formatting.
  • ▹ Managing payroll and other employee-related monetary data.

Conclusion

The monetary field and monetary widget in Odoo are essential for handling currency values accurately. By using these features, users can efficiently manage and display financial data with correct formatting, supporting both local and multi-currency requirements. With Odoo’s powerful currency management, managing monetary data becomes seamless and intuitive.

Make a Comment

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