Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
1 Trả lời
5551 Lượt xem

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

Ảnh đại diện
Huỷ bỏ

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

https://goo.gl/8HgnCF

Câu trả lời hay nhất

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

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
4
thg 5 24
12548
1
thg 4 24
3166
0
thg 11 23
1932
1
thg 9 23
2040
2
thg 8 23
4423