Hello Odoo Community,
I am working on Odoo 17 Online with Studio I am trying to calculate a field in the project.project model, which should sum the values of a field x_studio_valore_investimento in a x_beni_4_0_e_5_0 model (representing the assets associated with the project). Each asset is linked to the project via a relational field x_studio_progetto.
However, when I try to calculate this field using a computed field and a Python function, I get the following error:
css
Copia
ValueError: forbidden opcode(s) in 'class Project(models.Model): ...'
The error seems to be caused by the fact that the Python code contains a class definition within the context of the field calculation, which is not allowed. I have tried several approaches, but none seem to resolve the issue.
Details of my code:
I have defined the computed field in the project.project model as follows:
python
Copia
class Project(models.Model): _inherit = 'project.project' x_studio_valore_stimato_investimento = fields.Float( string="Estimated Investment Value", compute="_compute_valore_stimato_investimento", store=True ) @api.depends('x_studio_one2many_field_8v9_1iff9798r') # Depends on the relational field Many2one def _compute_valore_stimato_investimento(self): for record in self: assets = self.env['x_beni_4_0_e_5_0'].search([('x_studio_progetto', '=', record.id)]) total_investment = sum(assets.mapped('x_studio_valore_investimento')) record.x_studio_valore_stimato_investimento = total_investment
However, this code generates the error mentioned above.
What I have tried:
- I tried modifying the field calculation without redefining the Project class, but it doesn't seem to be sufficient.
- I reviewed the Odoo documentation and some online resources, but I couldn't find a solution for this specific error.
Questions:
- Is there a way to avoid the "forbidden opcode(s)" error when calculating fields in Odoo?
- How can I sum the values of the assets associated with the project without encountering this error?
- Is it possible to use a One2many relationship in a computed field like in my case?
I hope someone can help me resolve this issue. Thank you in advance for your support!
Additional Information:
- Odoo Version: 17.0
- Project Model: project.project
- Assets Model: x_beni_4_0_e_5_0
- Relational Field in the Assets Model: x_studio_progetto
- Field to Sum in the Assets Model: x_studio_valore_investimento
- Computed Field in the Project Model: x_studio_valore_stimato_investimento