Se rendre au contenu
Odoo Menu
  • Se connecter
  • Essai gratuit
  • Applications
    Finance
    • Comptabilité
    • Facturation
    • Notes de frais
    • Feuilles de calcul (BI)
    • Documents
    • Signature
    Ventes
    • CRM
    • Ventes
    • PdV Boutique
    • PdV Restaurant
    • Abonnements
    • Location
    Sites web
    • Site Web
    • eCommerce
    • Blog
    • Forum
    • Live Chat
    • eLearning
    Chaîne d'approvisionnement
    • Inventaire
    • Fabrication
    • PLM
    • Achats
    • Maintenance
    • Qualité
    Ressources Humaines
    • Employés
    • Recrutement
    • Congés
    • Évaluations
    • Recommandations
    • Parc automobile
    Marketing
    • Marketing Social
    • E-mail Marketing
    • SMS Marketing
    • Événements
    • Marketing Automation
    • Sondages
    Services
    • Projet
    • Feuilles de temps
    • Services sur Site
    • Assistance
    • Planification
    • Rendez-vous
    Productivité
    • Discussion
    • Validations
    • Internet des Objets
    • VoIP
    • Connaissances
    • WhatsApp
    Applications tierces Odoo Studio Plateforme Cloud d'Odoo
  • Industries
    Commerce de détail
    • Librairie
    • Magasin de vêtements
    • Magasin de meubles
    • Épicerie
    • Quincaillerie
    • Magasin de jouets
    Food & Hospitality
    • Bar et Pub
    • Restaurant
    • Fast-food
    • Guest House
    • Distributeur de boissons
    • Hotel
    Real Estate
    • Real Estate Agency
    • Cabinet d'architecture
    • Construction
    • Gestion immobilière
    • Jardinage
    • Association de copropriétaires
    Consulting
    • Accounting Firm
    • Partenaire Odoo
    • Agence Marketing
    • Cabinet d'avocats
    • Aquisition de talents
    • Audit & Certification
    Fabrication
    • Textile
    • Metal
    • Furnitures
    • Food
    • Brewery
    • Cadeaux d'entreprise
    Santé & Fitness
    • Club de sports
    • Opticien
    • Salle de fitness
    • Praticiens bien-être
    • Pharmacie
    • Salon de coiffure
    Trades
    • Bricoleur
    • Matériel informatique et support
    • Solar Energy Systems
    • Cordonnier
    • Services de nettoyage
    • HVAC Services
    Others
    • Nonprofit Organization
    • Agence environnementale
    • Location de panneaux d'affichage
    • Photographie
    • Leasing de vélos
    • Revendeur de logiciel
    Browse all Industries
  • Communauté
    Apprenez
    • Tutoriels
    • Documentation
    • Certifications
    • Formation
    • Blog
    • Podcast
    Renforcer l'éducation
    • Programme éducatif
    • Business Game Scale-Up!
    • Rendez-nous visite
    Obtenir le logiciel
    • Téléchargement
    • Comparez les éditions
    • Versions
    Collaborer
    • Github
    • Forum
    • Événements
    • Traductions
    • Devenez partenaire
    • Services for Partners
    • Enregistrer votre cabinet comptable
    Nos Services
    • Trouver un partenaire
    • Trouver un comptable
    • Rencontrer un conseiller
    • Services de mise en œuvre
    • Références clients
    • Assistance
    • Mises à niveau
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Obtenir une démonstration
  • Tarification
  • Aide

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

  • CRM
  • e-Commerce
  • Comptabilité
  • Inventaire
  • PoS
  • Projet
  • MRP
All apps
Vous devez être inscrit pour interagir avec la communauté.
Toutes les publications Personnes Badges
Étiquettes (Voir toutl)
odoo accounting v14 pos v15
À propos de ce forum
Vous devez être inscrit pour interagir avec la communauté.
Toutes les publications Personnes Badges
Étiquettes (Voir toutl)
odoo accounting v14 pos v15
À propos de ce forum
Aide

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

S'inscrire

Recevez une notification lorsqu'il y a de l'activité sur ce poste

Cette question a été signalée
purchasedevelopmentaccounting
2 Réponses
4278 Vues
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
Ignorer
Avatar
LandLogic IT s.n.c.
Meilleure réponse

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
Ignorer
Avatar
AmirAkbari
Auteur Meilleure réponse

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
Ignorer
Vous appréciez la discussion ? Ne vous contentez pas de lire, rejoignez-nous !

Créez un compte dès aujourd'hui pour profiter de fonctionnalités exclusives et échanger avec notre formidable communauté !

S'inscrire
Publications associées Réponses Vues Activité
Can I allow user to enter line-item extended price for Vendor Bills? Odoo 19
purchase accounting
Avatar
Avatar
Avatar
2
nov. 25
247
ODOO 18.3 create vendor bill, upload vendor bill Résolu
purchase accounting
Avatar
Avatar
Avatar
2
oct. 25
948
Restrict QWeb Report in Print Menu to Vendor Payments Only in Odoo 17
development accounting
Avatar
Avatar
1
oct. 25
533
odoo18 down payment
purchase accounting
Avatar
Avatar
Avatar
3
oct. 25
2717
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
oct. 25
1042
Communauté
  • Tutoriels
  • Documentation
  • Forum
Open Source
  • Téléchargement
  • Github
  • Runbot
  • Traductions
Services
  • Hébergement Odoo.sh
  • Assistance
  • Migration
  • Développements personnalisés
  • Éducation
  • Trouver un comptable
  • Trouver un partenaire
  • Devenez partenaire
À propos
  • Notre société
  • Actifs de la marque
  • Contactez-nous
  • Emplois
  • Événements
  • Podcast
  • Blog
  • Clients
  • Informations légales • Confidentialité
  • Sécurité.
الْعَرَبيّة 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 est une suite d'applications open source couvrant tous les besoins de votre entreprise : CRM, eCommerce, Comptabilité, Inventaire, Point de Vente, Gestion de Projet, etc.

Le positionnement unique d'Odoo est d'être à la fois très facile à utiliser et totalement intégré.

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