跳至内容
菜单
此问题已终结
2 回复
6339 查看

Hi !

I have write the an @api.onchange method in some Float field and the @api.onchange is calculation is working fine.

The problem is that when I click on save the system doesn't pick up the change and it all Float fields append to zero.

Does anybody knows how to do write() function of the model for @api.onchange function?

Below,i have mentioned the code

Thank you !

class LabTest(models.Model):
    _name = 'lab.test.line'
 
    line_id = fields.Many2one('quality.alert', string='perameter type')
    labtest_type_id=fields.Many2one('labtest.type', string='Test Name')   
    sample_value_per = fields.Float(string='Sample Value in Percentage')
    sample_value = fields.Float(string='Input Sample Value')


    @api.onchange('labtest_type_id','labtest_type_id.code','sample_value','sample_value_per')
    def onchange_value(self):
        for line in self:
            if line.labtest_type_id and line.labtest_type_id.code == 'ip' and line.sample_value:
                line.sample_value_per = float(line.sample_value/line.sample_value) * 100
            elif line.labtest_type_id and line.labtest_type_id.code == 'pp':
                ip_value = 1
                lp_lines = line.line_id.labtest_type_line.filtered(lambda r: r.labtest_type_id.code == 'ip')
                for ipln in lp_lines:
                    ip_value = ipln.sample_value
                line.sample_value_per = float(line.sample_value/ip_value) * 100
            elif line.labtest_type_id and line.labtest_type_id.code == 'ss':
                ip_value = 1
                lp_lines = line.line_id.labtest_type_line.filtered(lambda r: r.labtest_type_id.code == 'ip')
                for ipln in lp_lines:
                    ip_value = ipln.sample_value
                line.sample_value_per = float(line.sample_value/ip_value) * 100

形象
丢弃

I think else loop is missing in your condition and can you update your code after added else condition?

编写者

Thanks for your answer Manish, But no need else condition

最佳答案

use store= True in python end or force_save = "1" in xml end to save the field value.

  sample_value_per = fields.Float(string='Sample Value in Percentage', store=True)


形象
丢弃
编写者

Thanks for answer..

its working well but i facing one problem invisible is not working after put force_save = "1"

<field name="sample_value_per" force_save = "1" attrs="{'invisible': [('state', '=', 'draft')]}"/>

try this attrs="{'invisible': [('state', 'in', ['draft'])]}"

相关帖文 回复 查看 活动
1
10月 23
1793
2
10月 23
2039
2
8月 23
4016
4
8月 23
20294
3
10月 22
10770