Přejít na obsah
Menu
You need to be registered to interact with the community.
This question has been flagged
1 Odpovědět
5545 Zobrazení

Hi there

Using Odoo 13 CE, I built a custom module(x.cost) having several computed fields,

I am trying to update value from a computed field(u_cost) into product.template(cost_price) field

Here is the code

u_cost = fields.Float(string='my cost', compute='_ucost_calc', store=True)

@api.depends('total_cost', 'quantity')
def _ucost_calc(self):
for a in self:
        a.u_cost = a.total_cost / a.quantity

I'm trying to update this 'u_cost' value into the Product.template 'cost_price' field.

have tried cr.execute, self.env and most of the methods, but unable to get the u_cost value into the product.template cost_price field...

Being new to python coding, would appreciate exact code for this

Thank you in advance

Avatar
Zrušit

If you are new to odoo and python development, have a look at this:

https://goo.gl/8HgnCF

Nejlepší odpověď

Hi,

Try the following code

from odoo import models, fields, api


class XCost(models.Model):

    _name = 'x.cost'

    _description = 'X Cost'


    total_cost = fields.Float(string='Total Cost')

    quantity = fields.Float(string='Quantity')

    u_cost = fields.Float(string='My Cost', compute='_ucost_calc', store=True)

    product_id = fields.Many2one('product.template', string='Product')



    @api.depends('total_cost', 'quantity')

    def _ucost_calc(self):

        for a in self:

            if a.quantity != 0:

                a.u_cost = a.total_cost / a.quantity

                # Assuming there's a Many2one field linking to the product template, e.g., product_id

                if a.product_id:

                    a.product_id.cost_price = a.u_cost

            else:

                a.u_cost = 0.0


Hope it helps

Avatar
Zrušit
Related Posts Odpovědi Zobrazení Aktivita
4
kvě 24
12517
1
dub 24
3144
0
lis 23
1917
1
zář 23
2022
2
srp 23
4411