跳至內容
選單
此問題已被標幟
2 回覆
14134 瀏覽次數

Hello,

I am trying to populate a Selection field in v8 with values from a function when an onchange event is triggered.

When I print the output it looks fine, but then my selection field is empty and I am not getting any errors.

This is what my code looks like

The field:

myfield = fields.Selection(selection='_get_selection', string='What do you want ?') 


The function onchange


	@api.onchange('building')
def _get_selection(self):
choise = []
ret_ids = self.env['sim.mymod'].search([['cols', '=', self.building.id],])
for amen_id in ret_ids:
                                amenity_obj = self.env['sim.mymod'].browse(amen_id.id)
print amenity_obj.id
print amenity_obj.name
choise.append((amenity_obj.id), str(amenity_obj.name))
                                print choise  
return choise




This prints the following output in the console:

[[3, 'A'], [4, 'B']]

which is exactly what I need, but my field shows nothing.

Any idea will be deeply appreciated.

Thanks a lot!

頭像
捨棄
最佳答案

Hi,

Just make you function as like below.

	@api.onchange('building')
def _get_selection(self):
choise = []
ret_ids = self.env['sim.mymod'].search([['cols', '=', self.building.id],])
for amen_id in ret_ids:
                                amenity_obj = self.env['sim.mymod'].browse(amen_id.id)
print amenity_obj.id
print amenity_obj.name
choise.append(str(amenity_obj.id), str(amenity_obj.name))
                                print choise  
return choise

頭像
捨棄
最佳答案

maybe u can't get the value from onchange method because is not executed yet, onchange event come from view and the model consume ur _get_selection function before execute onchange method 

頭像
捨棄
相關帖文 回覆 瀏覽次數 活動
1
4月 15
6533
1
3月 21
7958
1
3月 21
3823
2
12月 17
16433
0
4月 15
3633