首页 > 解决方案 > python TypeError:'crm.lead.product'对象不能解释为整数,Odoo 14

问题描述

'lead_product_ids' 由产品列表组成,我试图将每个产品的 qty*price 单位相乘以获得总数,然后将所有总数相加。

错误:TypeError:“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

标签: pythonodoo

解决方案


看起来像

for i in rec.lead_product_ids:

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

所以,当你这样做

for all in range(i):

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

TypeError:“crm.lead.product”对象不能解释为整数

为了解决这个问题,你应该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

推荐阅读