如何计算odoo中的产品重量?

时间:2016-08-04 17:41:10

标签: python openerp odoo-8

我想计算odoo报价和销售订单中的总产品重量,但我不知道如何为它编写方法,有人可以给我一些暗示吗? here is in image total weight written in bottom

class ProductWeight(models.Model):
    _inherit = "sale.order"

    th_weight = fields.Float('Weight')

    @api.one
    def _calcweight(self):
        currentweight = 0
        for weight in self.weight_ids:
            currentweight = currentweight + weight.th_weight
        self.weight_total = currentweight

    weight_total = fields.Float('Total Weight', store=True, compute="_calcweight")

这是我计算总重量的方法,但不正确,给我404错误。

1 个答案:

答案 0 :(得分:2)

我建议使用以下代码

from openerp import models, fields, api

class ProductWeight(models.Model):
   _inherit = 'sale.order.line'
   th_weight = fields.Float(string='Weight',
                               store=True,
                               related='product_id.weight')

class ProductWeightOrder(models.Model):
    _inherit = 'sale.order'
    @api.depends('order_line.th_weight')
    def _calcweight(self):
        currentweight = 0
        for order_line in self.order_line:
            currentweight = currentweight + order_line.th_weight
        self.weight_total = currentweight

    weight_total = fields.Float(compute='_calcweight', string='Total Weight')

您必须在产品主数据中设置参数权重,无论如何您可以修改th_weight字段以使其与产品无关,用户必须填写此信息