Search code examples
pythonodooodoo-10many-to-one

How to dynamic change the domain of a Many2one field in ODOO


I want to dynamic change the domain of many2one field every time the value of dul changes #dul is Boolean

in the first case so that domain="[('parent_id', '!=', False), ('parent_id', '=', imeReona)]" , and in another case domain="[('parent_id', '!=', False)]"

I tried:

my_many2one_field = fields.Many2one(..., domain=lambda self: self.env['ir.config_parameter'].sudo().get_param('domen_mm2o'))

@api.onchange('dul') def _onchange_dul(self): if (self.dul == True): res = "[('parent_id','!=',False),('parent_id','=',imeReona)]" else: res = "[('parent_id','!=',False)]" self.env['ir.config_parameter'].sudo().set_param('domen_mm2o',res)

If I was not clear enough or did not formulate the question well, please let me know


Solution

  • I fixed the code. From my_many2one_field I removed , domain=lambda self: self.env['ir.config_parameter'].sudo().get_param('domen_mm2o')

    and updated

    @api.onchange('dul')
    def _onchange_dul(self):
        if (self.dul == True):
            res = "[('parent_id','!=',False),('parent_id','=',imeReona)]"
        else:
            res = "[('parent_id','!=',False)]"
        self.env['ir.config_parameter'].sudo().set_param('domen_mm2o',res)
    

    added the following code

        return{
            'domain': {'my_many2one_field': self.env['ir.config_parameter'].sudo().get_param('domen_mm2o')}}