Add Computed Fields to a Model in Odoo13

This blog deals with the concept of computed field in odoo version 13.We are going to discuss basically these points.

Concept of computed field
Computed field and function definition
Use of @api.depends in computed fields
Searching on computed fields
Inverse Function on computed fields

Concept of computed field

In most of the scenarios while we create a model,we will have some fields whose values are to be calculated. Such fields may depend on the fields of same model or from another,In such cases we can  use the compute function inside the field definition. Those fields in which the values are calculated by defining a compute function inside the field definition are called  computed fields.

How to write a computed field and function definition

Suppose we are working with a car rental management module,we have a field in our car model that is amount. The value of amount field should be calculated based on the quantity(no of cars) and unit price field 

So that we can define the amount field as a computed field

              amount=fields.Float(string=’Total’, compute=’_get_total’)

We can write the function definition for compute=’_get_total’ as follows.Note that you should always write the function definition before compute field.

    @api.depends(‘quantity’,’unit_price’)
     def_get_total(self):
     for record in self:   
            record.total=record.quantity*record.unit_price

We should make sure if the function will be invoked with store=true along with decorator depends.

Use of @api.depends in computed fields

The major purpose of @api.depends() comes when we set the computed fields as stored, that is there are some cases in which we have to store the compute field value in the database.

In such situations we will write the compute field definition with store=’True’.
When we write like this our compute function will be only called when the depends fields value is changed

For eg:Suppose if once the compute field is calculated and assigned with a value with store=True condition,if we again change the value of unit_price without @api.depends() the amount field will not be calculated .So we should give the dependency field along with condition for the function to work

Searching on computed fields

Computed fields cannot be searched by default, because there’s no database value for them.

If the result of our computed field always depends on a combination of other fields,we can use the @api.depends() decorator on the computed method and set the field as store=True

So we can write the computed field definition

amount=fields.Float(string=’Total’, compute=’_get_total’,search=’_search_value’)

def_search_value(self,operator,value):
        If operator == ‘like’;
            Operator = ‘ilike’
        return[(‘amount’,operator,value)]

Inverse Function on computed field

The Use of Inverse parameter is quite simple. Normally the computing fields are read-only because it computes the values on the fly from the record set. If we need to make a manual entry on compute field, that can be done by giving inverse function. So it  triggers the call of the decorated function when the field is written/”created”. It reverses the computation and sets the relevant fields.

upper = fields.Char(compute=’_compute_upper’,
                   inverse=’_inverse_upper’,
                   search=’_search_upper’)

 

api.depends(’employee_id’)
def _compute_upper(self):
   for rec in self:
       rec.upper = rec.employee_name.upper() if rec.employee_name else False


def _inverse_upper(self):
   for rec in self:
       rec.employee_name = rec.upper.lower() if rec.upper else False

Comments

zoritoler imol: It is truly a great and helpful piece of information. I?¦m glad that you just shared this helpful info with us. Please keep us informed like this. Thanks for sharing.".

Leave a Reply

Your email address will not be published. Required fields are marked *

© 2020 Zesty Beanz Pvt Ltd All Rights Reserved.