This question has been flagged
2 Replies
8024 Views

Hi, I'm trying to figure out how to return domains for various fields in a single function, with @onchange decorator. Is this possible or I have to do cascade functions like after the first onchange function I have to use another one for the other domain? can I use 2 times the @onchange decorators? I've tried that before but without positive results.


Regards




Avatar
Discard
Author Best Answer

@Niyas

That code seems good but when I tried in Odoo 11 didn't worked for my custom model, Also i have to return 2 or more domains. so the workaround i've used is a chain onchange-decorated functions. like 


@api.onchange(a_field)

def domainB(self)

  #build the domain string

   return domainB


@api.onchange(b_field)

def domainC(self)

  #build the domain string

  return domainC


Assuming I have to change the A and B fields to trigger the functions, I've used the form to guide the user through the proccess, making the B and C fields invisible if they don't choose a value for A field(required) first. 



Avatar
Discard
Best Answer

Hi,

I think in the onchange itself it is possible. See the function named _change_model_id in the gamification module.


@api.onchange('model_id')
def _change_model_id(self):
"""Force domain for the `field_id` and `field_date_id` fields"""
if not self.model_id:
return {'domain': {'field_id': expression.FALSE_DOMAIN, 'field_date_id': expression.FALSE_DOMAIN}}
model_fields_domain = [
('store', '=', True),
'|', ('model_id', '=', self.model_id.id),
('model_id', 'in', self.model_id.inherited_model_ids.ids)]
model_date_fields_domain = expression.AND([[('ttype', 'in', ('date', 'datetime'))], model_fields_domain])
return {'domain': {'field_id': model_fields_domain, 'field_date_id': model_date_fields_domain}}

Thanks

Avatar
Discard