Dejan Dakovic
Dejan Dakovic

Reputation: 155

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

Upvotes: 0

Views: 1730

Answers (2)

Dejan Dakovic
Dejan Dakovic

Reputation: 155

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

Upvotes: 0

Akshay
Akshay

Reputation: 649

@api.onchange('partner_id')
def apply_domain_customer(self):
    vehicles = self.env['fleet.vehicle'].search([('driver_id', '=', 
      self.partner_id.id)]).ids
    return {
        'domain': {
            'vehicle': [('id', 'in', vehicles)]
        }}

Upvotes: 2

Related Questions