Skip to Content
Odoo Menu
  • Sign in
  • Try it free
  • Apps
    Finance
    • Accounting
    • Invoicing
    • Expenses
    • Spreadsheet (BI)
    • Documents
    • Sign
    Sales
    • CRM
    • Sales
    • POS Shop
    • POS Restaurant
    • Subscriptions
    • Rental
    Websites
    • Website Builder
    • eCommerce
    • Blog
    • Forum
    • Live Chat
    • eLearning
    Supply Chain
    • Inventory
    • Manufacturing
    • PLM
    • Purchase
    • Maintenance
    • Quality
    Human Resources
    • Employees
    • Recruitment
    • Time Off
    • Appraisals
    • Referrals
    • Fleet
    Marketing
    • Social Marketing
    • Email Marketing
    • SMS Marketing
    • Events
    • Marketing Automation
    • Surveys
    Services
    • Project
    • Timesheets
    • Field Service
    • Helpdesk
    • Planning
    • Appointments
    Productivity
    • Discuss
    • Approvals
    • IoT
    • VoIP
    • Knowledge
    • WhatsApp
    Third party apps Odoo Studio Odoo Cloud Platform
  • Industries
    Retail
    • Book Store
    • Clothing Store
    • Furniture Store
    • Grocery Store
    • Hardware Store
    • Toy Store
    Food & Hospitality
    • Bar and Pub
    • Restaurant
    • Fast Food
    • Guest House
    • Beverage Distributor
    • Hotel
    Real Estate
    • Real Estate Agency
    • Architecture Firm
    • Construction
    • Estate Management
    • Gardening
    • Property Owner Association
    Consulting
    • Accounting Firm
    • Odoo Partner
    • Marketing Agency
    • Law firm
    • Talent Acquisition
    • Audit & Certification
    Manufacturing
    • Textile
    • Metal
    • Furnitures
    • Food
    • Brewery
    • Corporate Gifts
    Health & Fitness
    • Sports Club
    • Eyewear Store
    • Fitness Center
    • Wellness Practitioners
    • Pharmacy
    • Hair Salon
    Trades
    • Handyman
    • IT Hardware & Support
    • Solar Energy Systems
    • Shoe Maker
    • Cleaning Services
    • HVAC Services
    Others
    • Nonprofit Organization
    • Environmental Agency
    • Billboard Rental
    • Photography
    • Bike Leasing
    • Software Reseller
    Browse all Industries
  • Community
    Learn
    • Tutorials
    • Documentation
    • Certifications
    • Training
    • Blog
    • Podcast
    Empower Education
    • Education Program
    • Scale Up! Business Game
    • Visit Odoo
    Get the Software
    • Download
    • Compare Editions
    • Releases
    Collaborate
    • Github
    • Forum
    • Events
    • Translations
    • Become a Partner
    • Services for Partners
    • Register your Accounting Firm
    Get Services
    • Find a Partner
    • Find an Accountant
    • Meet an advisor
    • Implementation Services
    • Customer References
    • Support
    • Upgrades
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Get a demo
  • Pricing
  • Help

Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:

  • CRM
  • e-Commerce
  • Accounting
  • Inventory
  • PoS
  • Project
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
Tags (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Tags (View all)
odoo accounting v14 pos v15
About this forum
Help

Error in field calculation in Odoo 17: "forbidden opcode(s)" when summing values of assets associated with a project

Subscribe

Get notified when there's activity on this post

This question has been flagged
2 Replies
1605 Views
Avatar
Euroservis S.r.l.

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

0
Avatar
Discard
Avatar
Ray Carnes (ray)
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. 

1
Avatar
Discard
Euroservis S.r.l.
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.

Ray Carnes (ray)

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

Avatar
Marc Durepos
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!

1
Avatar
Discard
Euroservis S.r.l.
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!

Marc Durepos

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')
)
```

Marc Durepos

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

Euroservis S.r.l.
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!

Marc Durepos

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.

Euroservis S.r.l.
Author

Thank you!

Enjoying the discussion? Don't just read, join in!

Create an account today to enjoy exclusive features and engage with our awesome community!

Sign up
Community
  • Tutorials
  • Documentation
  • Forum
Open Source
  • Download
  • Github
  • Runbot
  • Translations
Services
  • Odoo.sh Hosting
  • Support
  • Upgrade
  • Custom Developments
  • Education
  • Find an Accountant
  • Find a Partner
  • Become a Partner
About us
  • Our company
  • Brand Assets
  • Contact us
  • Jobs
  • Events
  • Podcast
  • Blog
  • Customers
  • Legal • Privacy
  • Security
الْعَرَبيّة Català 简体中文 繁體中文 (台灣) Čeština Dansk Nederlands English Suomi Français Deutsch हिंदी Bahasa Indonesia Italiano 日本語 한국어 (KR) Lietuvių kalba Język polski Português (BR) română русский язык Slovenský jazyk slovenščina Español (América Latina) Español ภาษาไทย Türkçe українська Tiếng Việt

Odoo is a suite of open source business apps that cover all your company needs: CRM, eCommerce, accounting, inventory, point of sale, project management, etc.

Odoo's unique value proposition is to be at the same time very easy to use and fully integrated.

Website made with

Odoo Experience on YouTube

1. Use the live chat to ask your questions.
2. The operator answers within a few minutes.

Live support on Youtube
Watch now