Skip to Content
Odoo Menu
  • Log ind
  • Prøv gratis
  • Apps
    Økonomi
    • Bogføring
    • Fakturering
    • Udgifter
    • Regneark (BI)
    • Dokumenter
    • e-Signatur
    Salg
    • CRM
    • Salg
    • POS Butik
    • POS Restaurant
    • Abonnementer
    • Udlejning
    Hjemmeside
    • Hjemmesidebygger
    • e-Handel
    • Blog
    • Forum
    • LiveChat
    • e-Læring
    Forsyningskæde
    • Lagerbeholdning
    • Produktion
    • PLM
    • Indkøb
    • Vedligeholdelse
    • Kvalitet
    HR
    • Medarbejdere
    • Rekruttering
    • Fravær
    • Medarbejdersamtaler
    • Anbefalinger
    • Flåde
    Marketing
    • Markedsføring på sociale medier
    • E-mailmarketing
    • SMS-marketing
    • Arrangementer
    • Automatiseret marketing
    • Spørgeundersøgelser
    Tjenester
    • Projekt
    • Timesedler
    • Udkørende Service
    • Kundeservice
    • Planlægning
    • Aftaler
    Produktivitet
    • Dialog
    • Godkendelser
    • IoT
    • VoIP
    • Vidensdeling
    • WhatsApp
    Tredjepartsapps Odoo Studio Odoo Cloud-platform
  • Brancher
    Detailhandel
    • Boghandel
    • Tøjforretning
    • Møbelforretning
    • Dagligvarebutik
    • Byggemarked
    • Legetøjsforretning
    Mad og værtsskab
    • Bar og pub
    • Restaurant
    • Fastfood
    • Gæstehus
    • Drikkevareforhandler
    • Hotel
    Ejendom
    • Ejendomsmægler
    • Arkitektfirma
    • Byggeri
    • Ejendomsadministration
    • Havearbejde
    • Boligejerforening
    Rådgivning
    • Regnskabsfirma
    • Odoo-partner
    • Marketingbureau
    • Advokatfirma
    • Rekruttering
    • Audit & certificering
    Produktion
    • Tekstil
    • Metal
    • Møbler
    • Fødevareproduktion
    • Bryggeri
    • Firmagave
    Heldbred & Fitness
    • Sportsklub
    • Optiker
    • Fitnesscenter
    • Kosmetolog
    • Apotek
    • Frisør
    Håndværk
    • Handyman
    • IT-hardware og support
    • Solenergisystemer
    • Skomager
    • Rengøringsservicer
    • VVS- og ventilationsservice
    Andet
    • Nonprofitorganisation
    • Miljøagentur
    • Udlejning af billboards
    • Fotografi
    • Cykeludlejning
    • Softwareforhandler
    Gennemse alle brancher
  • Community
    Få mere at vide
    • Tutorials
    • Dokumentation
    • Certificeringer
    • Oplæring
    • Blog
    • Podcast
    Bliv klogere
    • Udannelselsesprogram
    • Scale Up!-virksomhedsspillet
    • Besøg Odoo
    Få softwaren
    • Download
    • Sammenlign versioner
    • Udgaver
    Samarbejde
    • Github
    • Forum
    • Arrangementer
    • Oversættelser
    • Bliv partner
    • Tjenester til partnere
    • Registrér dit regnskabsfirma
    Modtag tjenester
    • Find en partner
    • Find en bogholder
    • Kontakt en rådgiver
    • Implementeringstjenester
    • Kundereferencer
    • Support
    • Opgraderinger
    Github Youtube Twitter LinkedIn Instagram Facebook Spotify
    +1 (650) 691-3277
    Få en demo
  • Prissætning
  • Hjælp

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

  • CRM
  • e-Commerce
  • Bogføring
  • Lager
  • PoS
  • Projekt
  • MRP
All apps
Du skal være registreret for at interagere med fællesskabet.
All Posts People Emblemer
Tags (View all)
odoo accounting v14 pos v15
Om dette forum
Du skal være registreret for at interagere med fællesskabet.
All Posts People Emblemer
Tags (View all)
odoo accounting v14 pos v15
Om dette forum
Hjælp

Forbidden Opcode in Server Action when Trying to Update Fields in Odoo 16

Tilmeld

Få besked, når der er aktivitet på dette indlæg

Dette spørgsmål er blevet anmeldt
purchasedevelopmentaccounting
2 Besvarelser
4299 Visninger
Avatar
AmirAkbari


Hello,

I am facing an issue with Odoo 16 when trying to implement a calculation in a Server Action for the Purchase Order Line model. I have added a custom field called "Currency Exchange Rate" (x_studio_monetary_field_atjMK) where users manually enter the exchange rate. I want to use this exchange rate to multiply the unit price (price_unit) and update the subtotal (price_subtotal) accordingly. The calculation should be:

makefileCopy codeprice_subtotal = price_unit * exchange_rate

However, I am getting the following error:

"forbidden opcode(s) in 'lambda': STORE_ATTR"

I understand that this error occurs because certain actions like direct field updates (record.price_subtotal = ...) are restricted in Server Actions for security reasons.

Here is the code I used for the Server Action:

if record.x_studio_monetary_field_atjMK:
    record.price_subtotal = record.price_unit * record.x_studio_monetary_field_atjMK
else:
    record.price_subtotal = record.price_unit

Is there any workaround or a proper way to perform this calculation and update the price_subtotal using a Server Action in Odoo 16? I would also be open to using a computed field if it helps resolve this issue.

Any help would be greatly appreciated!

Thank you.


0
Avatar
Kassér
Avatar
LandLogic IT s.n.c.
Bedste svar

Explanation and Solution:

The error occurs because Odoo restricts certain operations, such as direct field assignment (record.price_subtotal = ...), in Server Actions for security reasons. Instead, you should use one of the following approaches:

Solution 1: Use a Computed Field

Computed fields are the best approach for dynamic calculations. Here's how you can implement it:

  1. Add the Computed Field in Your Custom Module: Define a new computed field in your custom module to calculate the subtotal:
    from odoo import models, fields, api
    
    class PurchaseOrderLine(models.Model):
        _inherit = 'purchase.order.line'
    
        x_currency_exchange_rate = fields.Float(string="Currency Exchange Rate")
        x_computed_subtotal = fields.Float(
            string="Computed Subtotal",
            compute="_compute_computed_subtotal",
            store=True
        )
    
        @api.depends('price_unit', 'x_currency_exchange_rate')
        def _compute_computed_subtotal(self):
            for line in self:
                if line.x_currency_exchange_rate:
                    line.x_computed_subtotal = line.price_unit * line.x_currency_exchange_rate
                else:
                    line.x_computed_subtotal = line.price_unit
    
  2. Update the View: Add the new field (x_computed_subtotal) to the Purchase Order Line form/tree view so it is visible to users.

Solution 2: Use a Button Action or Python Script

If you need this calculation as a one-time update (e.g., triggered by a button), create a method in a custom module:

  1. Define the Button Action in Your Model:
    from odoo import models, fields
    
    class PurchaseOrderLine(models.Model):
        _inherit = 'purchase.order.line'
    
        def update_subtotal(self):
            for line in self:
                if line.x_currency_exchange_rate:
                    line.price_subtotal = line.price_unit * line.x_currency_exchange_rate
                else:
                    line.price_subtotal = line.price_unit
    
  2. Add the Button to the View: Add a button to trigger the update:
    <record id="view_purchase_order_line_form_inherit" model="ir.ui.view">
        <field name="name">purchase.order.line.form.inherit</field>
        <field name="model">purchase.order.line</field>
        <field name="inherit_id" ref="purchase.view_order_line_form"/>
        <field name="arch" type="xml">
            <button name="action_view_stock_moves" position="after">
                <button name="update_subtotal"
                        string="Update Subtotal"
                        type="object"
                        class="btn-primary"/>
            </button>
        </field>
    </record>
    

Solution 3: Use a Server Action (with Context/Write Workaround)

If you must use a Server Action, avoid direct field assignment. Use write() instead:

  1. Modify the Server Action Code: Replace direct assignment with the following:
    if record.x_studio_monetary_field_atjMK:
        new_subtotal = record.price_unit * record.x_studio_monetary_field_atjMK
    else:
        new_subtotal = record.price_unit
    record.write({'price_subtotal': new_subtotal})
    
  2. Test the Action: Ensure this action works as expected on the Purchase Order Lines.

Preferred Approach: Computed Field

The computed field approach is recommended because it keeps calculations dynamic and avoids manual intervention. It also respects Odoo's ORM structure and ensures maintainability.

1
Avatar
Kassér
Avatar
AmirAkbari
Forfatter Bedste svar

In Solution 3, when an automatic operation is created on the purchase order model and the Python code is registered, this does not happen in the final result (when I create a new invoice and manually fill in the currency conversion rate field created by the studio, multiply it by the unit price and then does not show in total)

0
Avatar
Kassér
Enjoying the discussion? Don't just read, join in!

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

Tilmeld dig
Related Posts Besvarelser Visninger Aktivitet
Can I allow user to enter line-item extended price for Vendor Bills? Odoo 19
purchase accounting
Avatar
Avatar
Avatar
2
nov. 25
250
ODOO 18.3 create vendor bill, upload vendor bill Løst
purchase accounting
Avatar
Avatar
Avatar
2
okt. 25
957
Restrict QWeb Report in Print Menu to Vendor Payments Only in Odoo 17
development accounting
Avatar
Avatar
1
okt. 25
536
odoo18 down payment
purchase accounting
Avatar
Avatar
Avatar
3
okt. 25
2724
how to restrict the Employee from viewing the customer and vendor bill and payments and they only can do is they can genrate and create the new bills
purchase accounting
Avatar
Avatar
Avatar
4
okt. 25
1050
Community
  • Tutorials
  • Dokumentation
  • Forum
Open Source
  • Download
  • Github
  • Runbot
  • Oversættelser
Tjenester
  • Odoo.sh-hosting
  • Support
  • Opgradere
  • Individuelt tilpasset udvikling
  • Uddannelse
  • Find en bogholder
  • Find en partner
  • Bliv partner
Om os
  • Vores virksomhed
  • Brandaktiver
  • Kontakt os
  • Stillinger
  • Arrangementer
  • Podcast
  • Blog
  • Kunder
  • Juridiske dokumenter • Privatlivspolitik
  • Sikkerhedspolitik
الْعَرَبيّة 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 er en samling open source-forretningsapps, der dækker alle dine virksomhedsbehov – lige fra CRM, e-handel og bogføring til lagerstyring, POS, projektledelse og meget mere.

Det unikke ved Odoo er, at systemet både er brugervenligt og fuldt integreret.

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