官术网_书友最值得收藏!

Using a float field with configurable precision

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.

Getting ready

We will reuse the my_module addon module from Chapter 3, Creating Odoo Modules.

How to do it…

We need to install the decimal_precision module, add a "Usage" entry for our configuration, and then use it in the model field:

  1. Make sure the Decimal Accuracy module is installed; select Apps from the top menu, remove the default filter, search for the Decimal Precision Configuration app, and install it if it's not already installed:
    How to do it…
  2. Activate the Developer Mode in the About dialog box, available within the ? icon in the menu bar at the top. This will enable the Settings | Technical menu.
  3. Access the Decimal Precision configurations. To do this, open the Settings top menu and select Technical | Database Structure | Decimal Accuracy. We should see a list of the currently defined settings.
  4. Add a new configuration, setting Usage to Book Price and choosing the Digits precision:
    How to do it…
  5. Add the new dependency to the __openerp__.py manifest file. It should be similar to this:
    {   'name': 'Chapter 03 code',
     'depends': ['base', 'decimal_precision],
        'data': ['views/library_book.xml'] }
  6. To add the model field using this decimal precision setting, edit the 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))
    

How it works…

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.

主站蜘蛛池模板: 广德县| 武胜县| 柳河县| 铜山县| 罗平县| 读书| 江阴市| 青浦区| 濉溪县| 老河口市| 延津县| 曲靖市| 郸城县| 临桂县| 津市市| 龙岩市| 阿巴嘎旗| 伊宁市| 本溪| 乌鲁木齐市| 大宁县| 葫芦岛市| 佳木斯市| 延长县| 浦北县| 交口县| 遂平县| 改则县| 衡东县| 汉阴县| 邵武市| 台中县| 东乌| 朝阳市| 金堂县| 绍兴市| 宜宾县| 海淀区| 通城县| 台湾省| 海林市|