تخطي للذهاب إلى المحتوى
القائمة
لقد تم الإبلاغ عن هذا السؤال
1 الرد
7391 أدوات العرض

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
نوفمبر 24
2623
2
يوليو 22
11763
1
نوفمبر 19
6110
2
يونيو 15
8178
1
أبريل 20
5205