python TypeError: 'crm.lead.product' object cannot be interpreted as an integer, Odoo 14

python TypeError: 'crm.lead.product' object cannot be interpreted as an integer, Odoo 14

'lead_product_ids' 包含一个产品列表,我正在尝试将每个产品的数量*价格单位相乘以获得总数,然后将所有总数相加。

错误: 类型错误:'crm.lead.product' 对象不能解释为整数

代码

 @api.depends('lead_product_ids.qty', 'lead_product_ids.price_unit', 'lead_product_ids')
    def _compute_total_price(self):
        for rec in self:
            for i in rec.lead_product_ids:
                for all in range(i):
                    total = (all.qty * all.price_unit)
                    rec.total_qty_price_unit = sum(total) or 0

看起来像

for i in rec.lead_product_ids:

正在将 i 分配为 lead_product_ids 中每个产品的产品。

所以,当你这样做时

for all in range(i):

它将尝试执行 irange(),但 range() 需要整数输入——而不是产品对象,因此出现错误

TypeError: 'crm.lead.product' object cannot be interpreted as an integer

要解决此问题,您应该改用 i

@api.depends('lead_product_ids.qty', 'lead_product_ids.price_unit', 'lead_product_ids')
def _compute_total_price(self):
    for rec in self:
        for i in rec.lead_product_ids:
            total = (i.qty * i.price_unit)
            rec.total_qty_price_unit += total