Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
2 Odpowiedzi
6791 Widoki

Hi all,


I have noticed a behavior on some of my computed fields that I can't understand. I have the following field (toy example):

field_x = fields.Boolean(string="Field X", compute="_compute_field_x", store=True)


Then function  _compute_field_x is as follows:


@api.depends("field_y", "field_z") # none of these are computed def  _compute_field_x(self):
    for record in self:
        print('hi')
        if record.field_y = 'Something':
            record.field_x = "A"
        elif record.field_z = 'Something Else':
            record.field_x = "B"
        else:
            record.field_x = "C"
    

For some reason, whenever I change either field_y or field_z on the model's form view, the message "hi" is printed twice: once before the write() method is called (to update field_y or field_z), and then once again after the write method is concluded. And even stranger, is that when the method is called AFTER the write method, it record.field_y or record.field_z still show the values pre-write.

In my case, this is a problem, because I wanted to send an email warning when field_x changes, but if I do it on the compute method it will end up sending the email twice.

If someone could clarify this behavior for me, I'd really appreciate it!

Awatar
Odrzuć
Autor Najlepsza odpowiedź

In case this might help someone in the future - I was able to "fix" the problem of sending the email twice by checking if we are working in the local cache. I noticed the first time the compute function ran, the record being modified had an ID of type "odoo.models.NewId", which I assume is a temporary object Odoo uses  while the form is open, before actually writing to the database. So I simply check this and prevent the email functionality from running:

if not isinstance(record.id, models.NewId):

    send_email(...)

else:

    pass


Awatar
Odrzuć
Najlepsza odpowiedź

Hi, Regardless of the code you posted as the code has some syntax errors and will not work and I think you provided it as example.

The computed method  (if store = True) is called once one of the dependencies fields changes so it will be called once you change the value in the form and it will be invoked on a pseudo-record that contains the values present in the form and not yet saved to the DB (  pseudo-record ) so you will get the first message and once you save the record the write method called and the value of field_y or fieldz changes in DB so the computed  will be called again. 

BTW, if computed field is not stored the computed method will be called every time when the field is accessed.

To send your email I prefer to override write method and send your email whenever the field_x changes.


Awatar
Odrzuć
Autor

Hi Waleed, thanks for your comment, it clarified the situation.
Overwriting the "write" method was my initial approach, as I am already doing it with other fields. However, it seems that the write method is not invoked when a computed field is changed.
To explain better: when I changed field_y (to "Something", for example), field_x is recomputed, but the write method is only called once with values = {"field_y": "Something"}. The field_x does not show up. I am not sure how Odoo is writing field_x to the database in the backend, but it does not go through Model.write!

The computed method called every time if there is changes in the field and depend on your condition the field_x will be recomputed but the field_x will not shown in write method values. I'm not diving deep on how the computed field calculated and updated in DB but I think they update field_x using SQL Command. Will try to search and let you know

Autor

Thanks Waleed :) if the write method was called with the updated values, it would indeed fix my problem. For now I haven't found a solution but it's not critical.
Thanks for your effort!

The computed field is not shown in write method and it's save in cache and then set to the DB from cache. So you don't the calculated field you can check your condition directly from overrided write and create method.

Autor

Sorry, I don't understand what you mean by "So you don't the calculated field you can check your condition directly from overrided write and create method."!
But thanks for clarifying the cache thing, makes sense!

Sorry I mean if the field_x is only flag to send email, you don't want it just put the same condition you did in computed field in write method and send the email.

Autor

That was a good suggestion, unfortunately field_x is not a flag but rather a value. I want to send an email updating of the new value. I can probably hack some way to infer if the value changed or not during the write method :) I'll have to see if the additional complexity is worth it.
Thanks a lot!

Powiązane posty Odpowiedzi Widoki Czynność
1
cze 23
2623
0
kwi 24
1387
1
maj 23
2205
1
sty 23
1883
2
wrz 22
2488