Hi Paul San,
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 you need to make a manual entry on compute field, that can be done by giving inverse function. So it triggers call of the decorated function when the field is written/”created”. It reverses the computation and set 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
1.
2. Editing computed field
3. Inverse Function Triggered
Update:-
By default, a computed field is not stored to the database and is computed on-the-fly. Adding the attribute ``store=True`` will store the field's values in the database. The advantage of a stored field is that searching on that field is done by the database itself.
So computed fields which are not stored in the database can't be searched normally, To enable searching we have to define the search function explicitly. This can be achieved by adding ''search '' param with computing field. If we add a method to search on a compute field, the method is invoked when processing domains before doing an actual search on the model.
Another eg :
standard_price = fields.Float(
'Cost', compute='_compute_standard_price',
inverse='_set_standard_price', search='_search_standard_price',
digits=dp.get_precision('Product Price'), groups="base.group_user",
)
def _search_standard_price(self, operator, value):
products = self.env['product.product'].search([('standard_price', operator, value)], limit=None)
return [('id', 'in', products.mapped('product_tmpl_id').ids)]
Hello Hilar AK,
thank for your answer. The case you show me is quite clear and it's the same written in the official documentation but with you images examples now it's even clearer.
BUt I hafve these case where I have a field x_ids that have a m2m relation with another field.
In the compute function there is a search but in the inverse function what I have to written?
COuld you give me any suggestions?
I updated the answer and included details about search
Hilar AK
THank you.
Everytime you answer me I understand more and more.
Now I have this question:
OK i have to define a search function like you wrote me.
IN your example what could be the "_compute_standard_price" implementation?
it returns a price for the product after some calculations.