Adding a monetary field to a model
Odoo has special support for monetary values related to a currency. Let's see how we can use this in a model.
Getting ready
We will continue to use the my_library
add-on module from the previous recipe.
How to do it...
The monetary field needs a complementary currency field to store the currency for the amounts.
my_library
already has models/library_book.py
, which defines a basic model. We will edit this to add the required fields:
- Add the field to store the currency that is to be used:
class LibraryBook(models.Model): Â Â Â Â # ... Â Â Â Â currency_id = fields.Many2one( Â Â Â Â Â Â Â Â 'res.currency', string='Currency')
- Add the monetary field to store the amount:
class LibraryBook(models.Model): Â Â Â Â # ... Â Â Â Â retail_price = fields.Monetary( Â Â Â Â Â Â Â Â ...