When using float fields, we may want to let the end user configure the precision that is to be used. The Decimal Precision Configuration module addon provides this ability.
We will add a Cost Price field to the Library Book model, with a user-configurable number of digits.
We will reuse the my_module
addon module from Chapter 3, Creating Odoo Modules.
We need to install the decimal_precision
module, add a "Usage" entry for our configuration, and then use it in the model field:
__openerp__.py
manifest file. It should be similar to this:{ 'name': 'Chapter 03 code',
'depends': ['base', 'decimal_precision],
'data': ['views/library_book.xml'] }
models/library_book.py
file by adding the following:from openerp.addons import decimal_precision as dp # ... class LibraryBook(models.Model): # ... cost_price = fields.Float( 'Book Cost', dp.get_precision('Book Price))
The get_precision()
function looks up the name in the Decimal Accuracy Usage field and returns a tuple representing 16-digit precision with the number of decimals defined in the configuration.
Using this function in the field definition, instead of having it hardcoded, allows the end user to configure it according to his needs.