Se rendre au contenu
Menu
Cette question a été signalée
2 Réponses
6437 Vues

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
Ignorer

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

Auteur

Thanks for your answer Manish, But no need else condition

Meilleure réponse

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
Ignorer
Auteur

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

Publications associées Réponses Vues Activité
1
oct. 23
1935
2
oct. 23
2149
2
août 23
4141
4
août 23
20460
3
oct. 22
10934