2

我编写了一个自定义 odoo 模块,当我安装它时,它会抛出一个单例错误。当我注释掉 _compute_margin(self) 中的所有内容并返回 0.0 时,模块安装正常。然后,当我将 _compute_margin 函数更改回原始函数并更新模块时,它会更新得很好。

我需要进行哪些更改才能使用 _compute_margin 函数的内容正确安装模块?

class MyCompanyintercompanyMargin(models.Model):
    _name = 'my_companyintercompany.margin'
    name = fields.Char()
    x_marginsplitmodel = fields.Char()
    x_marginsplitdescription = fields.Char()

class ResPartner(models.Model):
    _name = 'res.partner'
    _inherit = 'res.partner'

    x_my_companyintercompany = fields.Boolean()
    x_my_companyintercompany_marginsplit = fields.Many2one(
        'my_companyintercompany.margin',
        string='Margin Split Model'
    )

    class SaleOrder(models.Model):
    _name = 'sale.order'
    _inherit = 'sale.order'

    x_endcustomer = fields.Many2one(
            comodel_name='res.partner',
            string='Customer'
    )

    x_my_companyintercompany_marginsplit = fields.Many2one(string='Margin Split', related="partner_id.x_my_companyintercompany_marginsplit")
    x_my_companyintercompany_marginsplitid = fields.Char(related="x_my_companyintercompany_marginsplit.name", string="Margin Split")

    x_prsmarginpercentage = fields.Float(string="Marge %")

    @api.depends('order_line.margin')
    def _compute_margin(self):
        amount_untaxed = self.amount_untaxed
        if self.x_my_companyintercompany_marginsplit:
            try:
                if self.x_my_companyintercompany_marginsplitid == "Total -2,5%":
                    totalordercost = 0.0
                    for line in self.order_line:
                        totalordercost += line.purchase_price * line.product_uom_qty
                    intercompanymargin = amount_untaxed * 0.025
                    self.x_prsmargin = amount_untaxed - totalordercost - intercompanymargin
                elif self.x_my_companyintercompany_marginsplitid == "Marge 50/50":
                    self.x_prsmargin = self.margin / 2
                else:
                    self.x_prsmargin = self.margin
            except:
                raise "Exception!"
        else:
            self.x_prsmargin = self.margin
        if amount_untaxed > 0.0:
              self.x_prsmarginpercentage = self.x_prsmargin / amount_untaxed * 100
        else:
               self.x_prsmarginpercentage = 0.0
    x_prsmargin = fields.Monetary(compute='_compute_margin', store='true')
4

2 回答 2

2

您需要使用@api.multi装饰您的计算方法。

基本上,当 Odoo 尝试初始化数据库中的新字段时,它会同时计算许多记录。您当前设置方法的方式一次只能支持一条记录,这就是您收到Expected singleton消息的原因。

尝试以下操作:

# First, include the @api.multi decorator
@api.multi
@api.depends('order_line.margin')
def _compute_margin(self):
    # Second, use a for loop to loop over self
    # because it's possible for self to be multiple records
    for order in self:
        # Finally, use your same method logic in the loop...

        # Except that you must assign the result **per record**
        # in the loop (with `order` in this example instead of `self`)
        order.x_prsmarginpercentage = ...

在Odoo ORM 文档中查看更多详细信息

于 2019-01-07T21:23:21.370 回答
2

将使用多个记录调用计算方法,这些记录在self. 但是您尝试直接从 获取属性self,这仅适用于单例(只有一条记录的记录集)。

因此,您有两种选择来解决问题:

  1. 将您的方法重写为 for-loop on self。您还应该用@api.multi. 简单的例子:
@api.multi
@api.depends('field1', 'field2', 'fieldn')
def _compute_my_field(self):
    for record in self:
        record.my_field = 4711
  1. 只需装饰您的计算方法,@api.one告诉 Odoo 为self. Odoo 将在每条记录上循环,self并将方法返回值收集到一个列表中,该列表将是循环结束时的返回值。您可以self以您已经使用的方式使用:作为单例。
于 2019-01-08T08:44:31.303 回答