【问题标题】:Computed Fields in Odoo 14Odoo 14 中的计算场
【发布时间】:2021-05-11 21:14:32
【问题描述】:

在 pos.order.line 中,我添加了一个布尔字段;我添加了 2 个字段计算; 我想根据布尔值将行总和分为 2 个字段:

“amount1”中布尔值为 False 的行的 (qty * price_unit) 总和
'amount2' 中布尔值为 True 的 (qty * price_unit) 行的总和

请问我该怎么做?

class PosOrder(models.Model):
    _inherit = "pos.order"


    amount1 = fields.Monetary(compute='_compute_amount1', readonly=True)
    amount2 = fields.Monetary(compute='_compute_amount2', readonly=True)

    @api.depends('payment_ids', 'lines')
    def _compute_amount_ht(self):
        for line in self.lines:
            if line.is_false == True:
                self.amount1 += line.qty * line.price_unit
            else:
                self.amount2 += line.qty * line.price_unit



class PosOrderLine(models.Model):
    _inherit = "pos.order.line"
    is_false = fields.Boolean(default=False)

【问题讨论】:

    标签: javascript python xml odoo


    【解决方案1】:

    您需要先迭代 self ,然后根据您的公式求和。然后将其分配回 pos.order 字段。例如,

    @api.depends('payment_ids', 'lines')
    def _compute_amount_ht(self):
        for order in self:
            amount1 = 0.0
            amount2 = 0.0
            for line in order.lines:
                if line.is_false == True:
                    amount1 += line.qty * line.price_unit
                else:
                    amount2 += line.qty * line.price_unit
            order.amount1 = amount1
            order.amount2 = amount2
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      • 2021-02-21
      • 2022-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多