Skip to Content
Menu
You need to be registered to interact with the community.
This question has been flagged
960 Prikazi

The setup:

This is in a custom module in Odoo 17 Community.

I have a model (model_A) which contains a One2many field (key_value_ids), which is a set of Key/Value pairs

There exists some complex business logic which restricts the co-existence of certain Key/Value pairs within a single record on model_A. I have a function (_validate_key_values) within the model_A class which applies the business logic and unlinks any records from the One2many field which do not meet the criteria. For the sake of a return value, the function always returns True.

To trigger the _validate_key_values function, I have a boolean field (compute_validate) on model_A. This is a computed field which looks to another function (_compute_validate_key_values) within the model_A class.

The code for _compute_validate_key_values look like this:

 @api.depends("key_value_ids")

 def_ compute_validate_key_values (self):

        self.compute_validate = self._validate_key_values()


The result:

When I add, remove or edit a linked record in the One2many field key_value_ids, the business logic is being applied correctly, and other key/value records are being unlinked appropriately. However, the change that is triggering the function is not being saved. If I'm adding a new key/value, the new value won't be in the list after the business logic has executed. If I delete a key/value, it will reappear after the business logic has executed. If I change the value of key/value, it will revert to the old value after the business logic has executed.


Avatar
Opusti

i this you should use force_save="1" in xml

Avtor

I tried using force_save="1" on the key_value_ids field in the xml. But the issue persists.

I have further noticed that if _validate_key_values does not unlink any records (i.e. all of the existing key/value pairs remain valid) then the change holds. So it's fair to conclude that something about performing an unlink on the same recordset that is being updated is causing the update to revert.

Is there a way to save a model from within a python function, so that I can save the current change before executing the business rules?

Avtor

A further observation. By setting breakpoints and stepping through the execution I can see that when the @api.depends field changes, the compute function is triggered twice. Once before the change and again after the change. It's interesting to see that UI updates independent of the model. If I add a new record to the One2many field key_value_ids, the new record will appear in the UI, then the compute function is triggered, but the new record will not be in the model, then the compute function is triggered again and the new record will be in the model.
None of this helps to solve my issue, but it's all jolly interesting!