我编写了一个自定义 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')