Help

0

Domain filtering many2one field dependent onchange from other field

Avatar
Theis Dræberg

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.

Avatar
Discard
1 Answer
0
Avatar
Niyas Raphy
Best Answer

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

2 Comments
Avatar
Discard
Avatar
Theis Dræberg
-

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

Avatar
Theis Dræberg
-

Note that we cant edit directly via .py file.

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