I need to change the values in the dropdown list when a radio button is changed. If direction is 'Export', the Shipper list should be from the Philippines, likewise, the Consignee list should not be from the Philippines.
This is in my model which declares the variables
direction = fields.Selection(([('import', 'Import'), ('export', 'Export')]), string='Direction')
shipper_id = fields.Many2one('res.partner', 'Shipper', domain=[('shipper', '=', True)])
consignee_id = fields.Many2one('res.partner', 'Consignee', domain= [('consignee', '=', True)])
This is in a code snippet of the XML view
<div class="oe_title mb24">
<h1>
<field class="text-break" name="name" default_focus="1" readonly="1"/>
</h1>
<h3>
<field name=" direction" widget="radio" required="1" options="{'horizontal': true}"/>
</h3>
</div>
<group>
<group string="Transporter">
<field name="shipper_id " required="1"/>
<field name="shipper_identifier"/>
</group>
<group string="Customer">
<field name="consignee_id" required="1"/>
<field name="consignee_identifier"/>
</group>
</group>
I added this in the model to change the domain
@api.onchange('direction')
def _onchange_direction(self):
philippines_id = 176
domain = {}
if self.direction == 'export':
self.shipper_id = False
self.consignee_id = False
domain = {
'shipper_id': [ ('shipper', '=', True), ('country_id', '=', philippines_id)],
'consignee_id': [('consignee', '=', True), ('country_id', '!=', philippines_id)],
}
else:
self.shipper_id = False
self.consignee_id = False
domain = {
'shipper_id': [('shipper', '=' , True)],
'consignee_id': [('consignee', '=', True)],
}
_logger.info('Freight domain: %s', domain)
return {'domain': domain}
When if I test it, based on the logs, it is printing the domain correctly. However nothing changes on the list in UI. Checking using the Developer, the domain is still the same, i.e. only [('shipper','=',True)]
When, I put it in the XML view directly the domain, the list will be correct.
<field name="shipper_id" required="1" domain="[('shipper', '=', True), ('country_id', '=', 176)]"/>
<field name="consignee_id" required="1" domain="[('consignee', '=', True), ('country_id', '!=', 176)]"/>
However, when I changed the direction, the list is the same, the domain did not changed. Meaning whatever is defined in the view, it will be stay like that even though the onchange is listened correctly. Even though there is a function being called, it was not applied. How can I fix that? I am using Odoo 17