Skip to main content
 首页 » 编程设计

python之为什么我的计算域在反面不起作用

2024年11月01日10bluestorm

我有计算字段 x_totalunity,它取决于字段 product_uom_qty 和 x_unitparcarton。

我想要的是能够在 x_totalunity 中写入任何值,并且 product_uom_qty 会因此发生变化(product_uom_qty = x_totalunity/x_unitparcarton).但这不起作用。

这是我的代码:

@api.depends('product_uom_qty', 'discount', 'price_unit', 'tax_id', 
             'x_unitparcarton'  ) 
def _compute_amount(self): 
""" 
compute the amounts of the SO line. 
""" 
for line in self: 
    line.x_totalunity = line.product_uom_qty * line.x_unitparcarton 
 
x_totalunity = fields.Float(compute='_compute_amount', string='Total unitéé', 
    readonly=False, store=True ,  required=True, copy=True) 

请您参考如下方法:

首先,从 @api.depends 中删除 discount、price_unit 和 tax_id,因为它们似乎与 x_totalunity 的计算无关。

然后,我认为您正在寻找参数 inverse,以在有人手动修改计算字段的情况下执行一些操作:

@api.multi 
@api.depends('product_uom_qty', 'x_unitparcarton') 
def _compute_x_totalunity(self): 
    """Compute the amounts of the SO line.""" 
    for line in self: 
        line.x_totalunity = line.product_uom_qty * line.x_unitparcarton 
 
@api.multi 
def _inverse_x_totalunity(self): 
    for line in self: 
        line.product_uom_qty = line.x_totalunity / line.x_unitparcarton 
 
x_totalunity = fields.Float( 
    compute='_compute_x_totalunity', 
    inverse='_inverse_x_totalunity', 
    string='Total unitéé', 
    required=True, 
    readonly=False, 
    store=True, 
    copy=True, 
)