odooodoo-14

How to force update a field from model?


I would like to know how to force update field price_subtotal in sale_order_line model. It is computed from several modules but I would like to be able to update it from my custom module. By clicking a button, the field gets updated to a certain number that I calculated.

I tried (creation part is successful)

sale_order_line = self.env['sale.order.line'].create({...})
sale_order_line.update({
  'price_subtotal': 1234,
})

I also tried putting the field update within the creation part. None of these methods update the field.

I need a way to force update the field.


Solution

  • You can not write computed fields not even forcefully. These fields are used in later processes and they have to be accurate and that is why it is computed.

    But if you want to update these fields anyway, there are a few ways to do that.

    1. Inherit Compute Method : You can inherit field's compute method in your custom module and change or overwrite calculation of it however you want.

    2. Overwrite Field : You can overwrite field in your custom module, defile field with same name in same module and do not write compute="" in definition. It will overwrite base field when your custom module is installed.

    3. Replace The Field : Create a new field in your custom module. Inherit view and hide base field and show your field there. This can be used for visibility purpose only. In next processes like invoices etc, original base field values will be taken.

    These are the ways you can manipulate calculation of base computed field in Odoo. But I do not recommend it, I suggest check configurations and check compute method to understand how you can get your desired value in that computed field.