Skip to Content
Menu
This question has been flagged
2 Replies
281 Views

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:

  1. I tried modifying the field calculation without redefining the Project class, but it doesn't seem to be sufficient.
  2. 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

Avatar
Discard
Best Answer

You cannot write traditional modules with Odoo Online.

See https://www.odoo.com/documentation/18.0/developer/tutorials/importable_modules.html for examples of what you can do. 

Avatar
Discard
Author

Thank you for your response.
In this case, in a standard module (project), it's about using the calculation section in the advanced settings of the field through Studio.

You can't define classes or use @api directives in custom fields.

Best Answer

Hello,

From your description and the error provided it seems  you are inserting this code into the "compute" field via the Odoo interface, when viewing the field in the technical settings (developer mode). If that's the case, you instead need to define only the body of the function within the "compute" field, and add your field x_studio_one2many_field_8v9_1iff9798r​ as a dependency (the field just above in the field form view).
If your code is actually within a module, there are some design problems you will want to consider since hard-coding Studio field names into a module is very likely to break. I don't think this is your case, but if it is, please consider moving all of this customization into an actual stand-alone module.

Best of luck!

Avatar
Discard
Author

Hi Marc,

Thank you for your help! You were absolutely right.

I have tried several solutions, including:

python

Copia

beni = self.env['x_beni_4_0_e_5_0'].search([('x_studio_progetto', '=', record.id)])
totale_investimento = sum(beni.mapped('x_studio_valore_investimento'))
record.x_studio_valore_stimato_investimento = totale_investimento

And

python

Copia

totale_investimento = sum(record.x_studio_one2many_field_8v9_1iff9798r.mapped('x_studio_valore_investimento'))
record.x_studio_valore_stimato_investimento = totale_investimento

But the system says it is too complex to calculate.

Do you happen to have a solution?

Thanks again for your support!

Hi again,
Have you defined the reciprocal field for your Many2one as a One2many field with its inverse_name='x_studio_one2many_field_8v9_1iff9798r' on the Project model as well? If not, you'll need to for the dependency. With that field present, say you call it `x_studio_beni_ids` you could simply use:

```
python

for project in self:
project.x_studio_valore_stimato_investimento = sum(
x_studio_beni_ids.mapped('x_studio_valore_investimento')
)
```

Apologies, I missed a project. before the x_studio_beni_ids in my previous comment. Nothing like writing naked code in a forum comment ;)

Author

Thank you so much!

I am not able to find a solution.

I tried

for project in self:
project.x_studio_valore_stimato_investimento = sum(
x_beni_4_0_e_5_0_ids.mapped('x_studio_valore_investimento'))

With the dipedendences x_studio_one2many_field_8v9_1iff9798r

Thank you anyway!

Your dependency is on the wrong field, but I'm not sure what other error you're getting. You should have x_beni_4_0_e_5_0_ids as the dependency.

Author

Thank you!