İçereği Atla
Menü
Bu soru işaretlendi
2 Cevaplar
2354 Görünümler


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.


Avatar
Vazgeç
En İyi Yanıt

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.

Avatar
Vazgeç
Üretici En İyi Yanıt

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)

Avatar
Vazgeç
İlgili Gönderiler Cevaplar Görünümler Aktivite
1
Ağu 25
268
1
Tem 25
323
2
Tem 25
331
1
Tem 25
1299
1
Haz 25
365