Ir al contenido
Menú
Se marcó esta pregunta
1 Responder
5532 Vistas

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
Descartar

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

https://goo.gl/8HgnCF

Mejor respuesta

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
Descartar
Publicaciones relacionadas Respuestas Vistas Actividad
4
may 24
12474
1
abr 24
3115
0
nov 23
1907
1
sept 23
2014
2
ago 23
4393