تخطي للذهاب إلى المحتوى
القائمة
لقد تم الإبلاغ عن هذا السؤال
3 الردود
6460 أدوات العرض

Hi everyone, another question. I am trying to setup a compute field that depends on the first_name attribute of my Person model, and has the value "Hi, ". I was able to achieve it in the following way, by looping through the "self" recordset:

```

hello_msg = fields.Char(string="hello", compute='_test_hello')

@api.depends("first_name")

def _test_hello(self):   

    for rec in self:      

        rec.hello_msg= 'Hi, ' + rec.name

``` 

However, it seems like any time I enter my list view, this field is recomputed for ALL my records, not just the one that I changed in the form view. This is just a toy problem, but for my actual use cases, this will cause a significant computational overhead. I wanted to trigger this function ONLY when a record changes, and only for that record.

Is there a way to achieve this?

Thanks


الصورة الرمزية
إهمال
أفضل إجابة

Hi Fred,

hello_msg = fields.Char(string="hello", compute='_test_hello', store=True)

update hello_msg field as storable, then compute function triggers only when the dependent field changes.

Regards

الصورة الرمزية
إهمال
الكاتب أفضل إجابة

@Rinkal thanks for your reply. However, I've tried doing it and it still triggered every time I entered the list view.

A couple more questions:

* doesn't your example iterate through all records anyway? Is there a way to target the single record that changes, instead of iterating through the self recordset?

* what is the point of  compute='_test_hello' on my original example? If we are manually assigning the a value to rec.hello_msg inside the function, we could very well assign values to other attributes, which makes me think that the compute field is useless? 

الصورة الرمزية
إهمال
أفضل إجابة

Hello,

You can use onchange() method instead of the compute method.

@api.onchange("first_name")
def _test_hello_based_on_change(self):
    for rec in self:
        rec.hello_msg= ''
        rec.hello_msg= 'Hi, ' + rec.name

الصورة الرمزية
إهمال
المنشورات ذات الصلة الردود أدوات العرض النشاط
1
يونيو 25
15478
3
أبريل 25
5685
Compute Fields تم الحل
2
يوليو 24
3355
1
يناير 24
1862
1
يوليو 22
2290