콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다

We need to domain filter options in a many2one field, based on selected value in another field.


Our setup example:


In Model A we have following many2one fields:

Product type (x_studio_field_C8Ncm)

Product (x_studio_field_y8xyP)


We need product to be domain filtered based on value set in product type.

Ie. if we choose product type: phone it has to filter options in products like iPhone, Galaxy etc. Not Macbook or whatever.


After extensively searching the forums and reading various linked documentations on api.onchange functions, I tried many compute field variations like this:


from odoo import api

@api.onchange('x_studio_field_C8Ncm')

def onchange_partner_id(self):

    for rec in self:

        return {'domain': {'x_studio_field_y8xyP': [('x_studio_field_C8Ncm_id', '=', self.x_studio_field_C8Ncm.id)]}}


With dependency:  x_studio_field_C8Ncm

However it returns error: 

raise ValueError("forbidden opcode(s) in %r: %s" % (expr, ', '.join(opname[x] for x in codes)))
ValueError: forbidden opcode(s) in "from odoo import api\r\n@api.onchange('x_studio_field_C8Ncm')\r\ndef onchange_partner_id(self):\r\n    for rec in self:\r\n        return {'domain': {'x_studio_field_y8xyP': [('x_studio_field_C8Ncm_id', '=', self.x_studio_field_C8Ncm.id)]}}": IMPORT_NAME, IMPORT_FROM


We are running Odoo 13.0 and odoo studio.


Ps. we also have a many2many field where we want similar domain filtering based on other field value. But may put that in different question if the same function cant be applied?


Please help.

아바타
취소
베스트 답변

Hi,

From the below line change the self to rec and try,


return {'domain': {'x_studio_field_y8xyP': [('x_studio_field_C8Ncm_id', '=', self.x_studio_field_C8Ncm.id)]}}

Change it to

return {'domain': {'x_studio_field_y8xyP': [('x_studio_field_C8Ncm_id', '=', rec.x_studio_field_C8Ncm.id)]}}


Also you can get more info here: How To Give Domain For A Field Based On Another Field

Thanks

아바타
취소
작성자

Thanks for your quick reply. However it still returns same error.

작성자

Note that we cant edit directly via .py file.

But are using compute field via odoo studio. If that makes a difference?

관련 게시물 답글 화면 활동
2
11월 24
2979
2
7월 22
12069
1
11월 19
6312
2
6월 15
8375
1
4월 20
5450