Skip to Content
Menu
This question has been flagged
1 Reply
3106 Views

I have created a custom settings model. In this model, I have a Float field that stores a rate. When I store the value in 'ir.config_parameter', it is a float. But when I retrieve the value, it comes back as a string.

My settings:

class CustomSettings(models.TransientModel):
    _name = 'custom.settings'
    _inherit = 'res.config.settings'

    rate = fields.Float()

    def set_values(self):
        res = super(CustomSettings, self).set_values()
        self.env['ir.config_parameter'].set_param('custom.rate', rate)
        return res

    @api.model
    def get_values(self):
        res = super(CustomSettings, self).get_values()
        settings = self.env['ir.config_parameter']
        rate = settings.get_param('custom.rate')
        res.update(rate=rate)
        return res
I can input a value into 'rate' from my view and then save it, which calls set_values(). But when I go back to the settings page, it gives me an error, saying
Error: [_.sprintf] expecting number but found string
Sure enough, printing the type of 'rate' when I retrieve it from 'ir.config_parameter' shows that it is a string.

Why is this happening? I know the easy workaround would be to just convert the value to a float when I retrieve it. But I feel like this shouldn't be necessary. Any thoughts?

Avatar
Discard
Best Answer

only convert string to float

 res.update(rate=float(rate)) 

any get_values default is str but u can convert to your data type  

Avatar
Discard