Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
1 Trả lời
3712 Lượt xem

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 , 


Ảnh đại diện
Huỷ bỏ

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

Câu trả lời hay nhất

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

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
3
thg 5 22
4966
0
thg 3 15
8910
1
thg 5 24
2621
9
thg 6 22
59596
1
thg 3 21
4128