Search code examples
pythonodooodoo-12

Odoo Studio Server Error Computed Fields, Error


I keep getting this error , what am I missing?
I've tried multiple approaches and I always get this error I am sure im missing something simple.
Example image of Odoo setup in image link

Odoo Compute Settings

from odoo import api
x_studio_field_CKrxZ = fields.Float(compute='compute_product_dimension', store=True)
@api.depends('list_price','standard_price')
def compute_product_dimension(self):
  for record in self:
      record['compute_product_dimension'] = record.list_price + record.standard_price 

Error in Odoo

ValueError: forbidden opcode(s) in "from odoo import api\r\nx_studio_field_CKrxZ = fields.Float(compute='compute_product_dimension', store=True)\r\[email protected]('list_price','standard_price')\r\ndef compute_product_dimension(self):\r\n  for record in self:\r\n      record['compute_product_dimension'] = record.list_price + record.standard_price \r\n": IMPORT_NAME, IMPORT_FROM

Solution

  • The computed field function is executed in Odoo's sandboxed implementation of eval called safe_eval. It forbids certain Python interpreter opcodes to prevent arbitrary code execution. The error you are getting is because the IMPORT_NAME and IMPORT_FROM opcodes are not allowed (caused by the from odoo import api statement).

    You should not need the import statement, field declaration, @api.depends decorator or the compute function signature definition, your compute method should look something like this:

    for record in self:
        record['x_studio_field_CKrxZ'] = record.list_price + record.standard_price 
    

    Instead of the @api.depends('list_price','standard_price'), the field's dependencies should be declared in the Dependencies field under Advanced Properties, which you already did as seen in the screenshot.