コンテンツへスキップ
メニュー
この質問にフラグが付けられました
6 返信
9046 ビュー

Hello,

My code was working on v7 but when I move it to v8 I am getting an "is not JSON serializable" error

All I want to do is populate a Many2One field when another field in the view changes.

Here is the code which is giving me the error:


 amenity = fields.Many2one('sim.amenities','Payments Cols',required=True,ondelete='cascade')


@api.onchange('building')
def modify_amenities(self):
     res = {'value':{'amenities':False}}
     ret_ids = []
     if self.building:
          ret_ids = self.env['sim.amenities'].search([['cols', '=', self.building.id],])
ret_lines = []
              for amen_id in ret_ids:
ret_sub_list = []
ret_sub_list.append(0)
ret_sub_list.append(False)
ret_lines_dict = {}
amenity_obj = self.env['sim.amenities'].browse(amen_id.id)
print amenity_obj
                ret_lines_dict.update({'name':amenity_obj.name})
ret_sub_list.append(ret_lines_dict)
ret_lines.append(ret_sub_list)
               res = {'value':{'amenity':ret_lines}}
self.amenity = res


Any idea why I might be getting the error or in any case how can I populate the field amenity when the field building changes?

Any tip will be really appreciated! Thanks a lot

アバター
破棄

in v8. you can directly write value inside the current record using self. you don't need to assign dictionary.

最善の回答

Hi,

Here you are passing a list of values to a many2one field, that’s why you are getting JSON error.

You have to pass the 'id' of many2one record you want to set there. For Eg: (here amenity_id contains id of wanted record)

self.amenity = amenity_id 


Hope this helps.............

アバター
破棄
著作者

Hi! thanks! the thing is that I want the field amenity to contain a list of values, not just one value so the user can chose one of them.

著作者

I see now. I added the code you provided (self.amenity = amenity_id ) inside the foreach. The problem I am having now is that the Many2one field gets all the values, not only the ones I am passing. Is there any way I can delete all the other values? Thanks again!

i have the same issue,Yakito did you get answer ?

最善の回答

i have resolved the issue 

you can return the domain , hope this can help you 

return {'domain':{'amenity': [('id', 'in', ret_lines)]}} 
アバター
破棄
関連投稿 返信 ビュー 活動
0
12月 18
13
1
5月 16
4822
1
3月 15
4549
1
9月 23
7001
3
8月 23
7455