Skip to Content
Menu
You need to be registered to interact with the community.
This question has been flagged

Hello All 

first of all , thanks in advanced :)


I have the Following 2 fields:

freight_type = fields.Selection(
[('Ocean', 'Ocean'), ('Air', 'air'), ('Land', 'Land')],
default='',
required=True
)


ship_type = fields.Selection(
[('FCL', 'fcl'), ('LCL', 'lcl'), ('Air', 'air'), ('Land', 'land')],
default='',
required=True
)
What I tried:

@api.onchange('freight_type')
def onchange_freight_type(self):
for rec in self:
if rec.freight_type == 'Ocean':
rec.ship_type = 'FCL'
elif rec.freight_type == 'Air':
rec.ship_type = 'Air'
elif rec.freight_type == 'Land':
rec.ship_type = 'Land'
else:
rec.ship_type = ''


Not what I need, cause  rec.ship_type may have values 

[('FCL', 'fcl'), ('LCL', 'lcl')] or one value
[('Air', 'air')]



What I Need :

When I choice value 'Ocean' from field  freight_type I want to filter ship_type to show only values

[('FCL', 'fcl'), ('LCL', 'lcl')]


When I choice value 'Air' from field  freight_type I want to filter ship_type to show only values

[('Air', 'air')]


When I choice value 'Land' from field  freight_type I want to filter ship_type to show only values

[('Land', 'land')]


So, the field ship_type  will show values which depends on field  freight_type 


regards , 


Avatar
Opusti

If you don't need to show it in real time, did you think to do it in a computed field instead of the onchange

Best Answer

Hi,

Hi,

If you have limited selection options, you can achieve this by creating separate selection fields for each option and making them visible in XML based on the condition.

Python (.py):
freight_type = fields.Selection(
[('Ocean', 'Ocean'), ('Air', 'Air'), ('Land', 'Land')],
string='Freight Type',
default='',
required=True
)
ship_type_ocean = fields.Selection(
selection=[('FCL', 'FCL'), ('LCL', 'LCL')],
string='Ship Type'
)

ship_type_air = fields.Selection(
selection=[('Air', 'Air')],
string='Ship Type'
)
ship_type_land = fields.Selection(
selection=[('Land', 'Land')],
string='Ship Type'
)
ship_type_default = fields.Selection(
selection=[('Default', 'Default')],
string='Ship Type'
)

XML:

<field name="freight_type"/>
<field name="ship_type_ocean" invisible="freight_type != 'Ocean'" required="freight_type == 'Ocean'"/>
<field name="ship_type_air" invisible="freight_type != 'Air'" required="freight_type == 'Air'"/>
<field name="ship_type_land" invisible="freight_type != 'Land'" required="freight_type == 'Land'"/>


Hope it helps

Avatar
Opusti
Related Posts Odgovori Prikazi Aktivnost
3
maj 22
4965
0
mar. 15
8907
1
maj 24
2621
9
jun. 22
59592
1
mar. 21
4127