Odoo 10 Implementation Cookbook: Explore the capabilities of Odoo and discover all you need to implement it

Creating a computed field

Technically, computed fields are the function fields that compute the values based on the value of other fields. You will be able to write the Python code to compute the value of the field at the time of creating a computed field using Odoo Studio.

Make sure you are in debug mode before you enter studio mode, it will be easy for us to get the technical name of any field from the tooltips available on the field.

Let's take a simple example to add a balance field, that computes the difference of Total Receivable and Total Payable for customer.

Go to the customer's form view, enter into studio mode, drag and drop the Monitory field below the Tags fields, and change the label of the field to Balance to Receive:

Enter A positive balance is advance payment from the customer and negative balance is receivable form customer in the Help Tooltip field to give a clear message to end-users.

To write the code to compute the balance, select field and on the left panel, click on More. You will be recited to the details of the field, as shown in the following screenshot:

There are three important fields that you have to configure as follows:

  • Stored: If it is set to False, by default it is set to True
  • Dependencies: To execute the compute process Odoo needs either a field or list of fields comma separated, when the value of those fields changes, the compute methods triggers on that record
  • Compute: We can write the Python code that will be executed to compute the value of the field

In our case, the value of the dependency fields will be debit, credit, and invoice_ids. Write the following code in the compute method:

    for record in self:      record['x_studio_field_wtvw3'] = record.debit - record.credit 

Once you are done with the configuration, create some customer and vendor bills for the same customer. The balance will be computed automatically; a small piece of code generates this big feature in Odoo.

Назад: Debug mode
Дальше: Organizing the menu structure
Показать оглавление

Комментариев: 1

Оставить комментарий

  1. hakim.ghenami@gmail.com