Book Image

Odoo Development Cookbook

By : Holger Brunn, Alexandre Fayolle, Daniel Reis
Book Image

Odoo Development Cookbook

By: Holger Brunn, Alexandre Fayolle, Daniel Reis

Overview of this book

Odoo is a full-featured open source ERP with a focus on extensibility. The flexibility and sustainability of open source is also a key selling point of Odoo. It is built on a powerful framework for rapid application development, both for back-end applications and front-end websites. The book starts by covering Odoo installation and administration, and provides a gentle introduction to application development. It then dives deep into several of the areas that an experienced developer will need to use. You’ll learn implement business logic, adapt the UI, and extend existing features.
Table of Contents (23 chapters)
Odoo Development Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

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.

Note

The Monetary field was introduced in Odoo 9.0 and is not available in previous versions. If you are using Odoo 8.0, the float field type is your best alternative.

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:

  1. Add the field to store the currency that is to be used:

    class LibraryBook(models.Model):
        # ...
        currency_id = fields.Many2one(
            'res.currency', string='Currency')
    
  2. Add the monetary field to store our amount:

    class LibraryBook(models.Model):
        # ...
        retail_price = fields.Monetary(
            'Retail Price',
            # optional: currency_field='currency_id',
            ...