odooodoo-17

Odoo 17 - Having a problem with returning the domain for the field when executing the onchange method


I'm having a problem with returning the domain for the field when executing onchange method. For the code below, it works fine on Odoo version 14. But on Odoo version 17, it doesn't work.

class ChooseDeliveryCarrier(models.TransientModel):
    _inherit = 'choose.delivery.carrier'
    
    service_id = fields.Many2one('ahamove.service', string='Service')
    service_request_ids = fields.Many2many('ahamove.service.request', 'estimate_request_rel',
                                           'choose_id', 'request_id', string='Request Type')
    @api.onchange('service_id')
    def _onchange_service_id(self):
       for rec in self:
          if rec.service_id:
             return {'domain': {'service_request_ids': [('service_id', '=', rec.service_id.id)]}}

This relationship between service_id and service_request_ids are One2Many

class AhamoveService(models.Model):
    _name = 'ahamove.service'
    request_ids = fields.One2many('ahamove.service.request', 'service_id', 'Requests')

class AhamoveServiceRequest(models.Model):
    _name = 'ahamove.service.request'
    service_id = fields.Many2one('ahamove.service', string='Service')

Can you guys give me a solution to do this? I want the service_request_ids field to display only according to the selected service_id.


Solution

  • Use a computed field as a domain for the service_request_ids field

    Example:

    Python:

    service_request_ids_domain = fields.Binary(compute="_compute_request_domain")
        
    @api.depends('service_id')
    def _compute_request_domain(self):
        for rec in self:
            domain = []
            if rec.service_id:
                domain = [('service_id', '=', rec.service_id.id)]
            rec.service_request_ids_domain = domain
    

    XML:

    <field name="service_request_ids_domain" invisible="True"/>
    <field name="service_request_ids" domain="service_request_ids_domain"/>