This question has been flagged
2 Replies
4734 Views

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

Avatar
Discard

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

Author

Thanks for your answer Manish, But no need else condition

Best Answer

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)


Avatar
Discard
Author

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'])]}"