Monetary fields are used to store financial amounts along with their
corresponding currencies. In Odoo, there is built-in support for
managing monetary values by linking them to a specific currency.
These fields can be defined either as monetary fields or as float
fields with a monetary widget for display.
Start by creating a Python file and adding the following code:
from odoo import api, fields, models
class FeeInherit(models.Model):
_inherit = "school.student"
company_id = fields.Many2one(
'res.company',
string="Company",
copy=False,
default=lambda self: self.env.user.company_id.id
)
currency_id = fields.Many2one(
'res.currency',
string="Currency",
related='company_id.currency_id'
)
tuition_amount = fields.Monetary(
string='Tuition Amount',
currency_field='currency_id'
)
In this example:
- The company_id field links the student to the
current user’s company.
- The currency_id field fetches the currency used
by that company.
- The tuition_amount field stores the monetary
amount and is linked to currency_id to ensure the correct
currency is displayed.
<form>
<sheet>
<group>
<group>
<field name="currency_id" invisible="1"/>
<field name="tuition_amount"/>
</group>
</group>
</sheet>
</form>
Instead of using a Monetary field in the Python file, you can define
the fee field as a simple Float and use the monetary widget in the
XML view:
<field name="tuition_amount" widget="monetary"/>