Passa al contenuto
Menu
È necessario essere registrati per interagire con la community.
La domanda è stata contrassegnata
1 Rispondi
3699 Visualizzazioni

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
Abbandona

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

Risposta migliore

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
Abbandona
Post correlati Risposte Visualizzazioni Attività
3
mag 22
4957
0
mar 15
8901
1
mag 24
2618
9
giu 22
59590
1
mar 21
4123