Search code examples
pythonpython-2.7global-variablesodooodoo-10

How to use a global variable to update a selection field with Odoo 10?


Hi is it possible to have a global variable which gets updated based on 'onchange' and assign the same to a selection field?

variable = []

class a:
    @api.onchange('some_other_filed')
    def function1(self):
        global variable
        variable = #something based on some other filed

    test = fields.Selection(variable)

Will this onchange reflect in selection field. All I get is empty list in selection field.


Solution

  • Using global variables is not recommended. You can use a persistent field on the ir.config_parameter table if you want to make it always available from anywhere in Odoo. You can see the stored configuration parameters on Settings > Technical > Parameters > System Parameters.

    You can set the value of the parameter like this:

    self.env['ir.config_parameter'].set_param('use_journal_setting', True)
    

    And get some other value like this:

    location = self.env['ir.config_parameter'].get_param(
        'ir_attachment.location'
    )
    

    You can also update the selection field directly with some value, or you can use the parameter to update it.

    @api.onchange('field_name')
    def onchange_field_name(self):
        self.test = self.env['ir.config_parameter'].get_param('parameter_name')
    

    Note: If you need many of these global variables, consider creating your own parameters table.