- Odoo Development Cookbook
- Holger Brunn Alexandre Fayolle Daniel Reis
- 291字
- 2021-07-16 11:00:33
Adding a monetary field to a Model
Odoo has special support for monetary values related to a currency. Let's see how to use it in a Model.
Getting ready
We will reuse the my_module
addon module from Chapter 3, Creating Odoo Modules.
How to do it…
The monetary field needs a complementary currency field to store the currency for the amounts.
The my_module
already has a models/library_book.py
defining a basic Model. We will edit this to add the needed 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 our amount:
class LibraryBook(models.Model): # ... retail_price = fields.Monetary( 'Retail Price', # optional: currency_field='currency_id', )
Now, upgrade the addon module, and the new fields should be available in the Model. They won't be visible in views until they are added to them, but we can confirm their addition by inspecting the Model fields in Settings | Technical | Database Structure | Models.
How it works…
Monetary fields are similar to Float fields, but Odoo is able to represent them correctly in the user interface since it knows what their currency is through a second field for that purpose.
This currency field is expected to be named currency_id
, but we can use whatever field name we like as long as it is indicated using the currency_field
optional parameter. You might like to know that the decimal precision for the amount is taken from the currency definition (the decimal_precision
field of the res.currency
model).
- Mastering AWS Lambda
- DevOps for Networking
- JavaScript+jQuery網頁特效設計任務驅動教程(第2版)
- Python金融數據分析
- 名師講壇:Java微服務架構實戰(SpringBoot+SpringCloud+Docker+RabbitMQ)
- 大學計算機基礎(第2版)(微課版)
- 從Excel到Python:用Python輕松處理Excel數據(第2版)
- 網站構建技術
- Learning OpenStack Networking(Neutron)(Second Edition)
- 深入分布式緩存:從原理到實踐
- Android移動開發案例教程:基于Android Studio開發環境
- ExtJS Web應用程序開發指南第2版
- Python趣味編程與精彩實例
- Instant Pygame for Python Game Development How-to
- Java面向對象程序設計教程