在odoo中更新列表时如何在方法中退出ZeroDivisionError?

how to exipt ZeroDivisionError inside a method while updating list in odoo?

我正在尝试 return 一个列表以在 pdf 报告中打印它,并且 return 给了我正确的数据但是如果产品成本为零,我得到

won_perecnt = (total_won * 100) / total_cost

division by zeroZeroDivisionError: float

如果发生错误,我需要发出警告,说明必须更改某些产品成本。

这是代码

@api.multi
def product_summary(self,):
    product_summary_dict = {}
    data=[]
    if self.date_from and self.date_to:
        order_detail = self.env['pos.order'].search([('date_order', '>=',self.date_from),
                                                     ('date_order', '<=', self.date_to)])
        if order_detail:
            for each_order in order_detail:
                for each_order_line in each_order.lines:
                    if each_order_line.product_id.name in product_summary_dict:
                        product_qty = product_summary_dict[each_order_line.product_id.name]
                        product_qty += each_order_line.qty
                        total_sales = each_order_line.price_unit * product_qty
                        total_cost = each_order_line.product_id.standard_price*product_qty
                        total_won = total_sales - total_cost
                        won_perecnt= (total_won *100)/ total_cost
                        res1= {
                            "name": each_order_line.product_id.name,
                            "sold_qty": product_qty,
                            "price_unit": each_order_line.price_unit,
                            'total_sales': total_sales,
                            "total_cost": total_cost,
                            "total_won": total_won,
                            "won_perecnt": won_perecnt,

                        }
                        data.append(res1)
                    else:
                        product_qty = each_order_line.qty
                        total_sales = each_order_line.price_unit * product_qty
                        total_cost = each_order_line.product_id.standard_price * product_qty
                        total_won = total_sales - total_cost
                        won_perecnt= (total_won *100)/ total_cost
                        res2 = {
                            "name": each_order_line.product_id.name,
                            "sold_qty": product_qty,
                            "price_unit": each_order_line.price_unit,
                            'total_sales': total_sales,
                            "total_cost": total_cost,
                            "total_won": total_won,
                            "won_perecnt": won_perecnt,
                        }
                        data.append(res2)
                    product_summary_dict[each_order_line.product_id.name] = product_qty;
    if data:
        return data
    else:
        return {}

任何帮助将不胜感激

将其包装在 try/catch 块中:

try:
    won_perecnt = (total_won * 100) / total_cost
catch ZeroDivisionError:
    print("A warning")

用更有用的东西替换 print()

做这个改变:

try:
    won_percent = (total_won * 100) / total_cost
except ZeroDivisionError:
    won_percent = None

print(won_percent)

Returns Nonetotal_cost 为零时。

完整代码:

@api.multi
def product_summary(self):
    product_summary_dict = {}
    data=[]
    if self.date_from and self.date_to:
        order_detail = self.env['pos.order'].search([('date_order', '>=',self.date_from),
                                                     ('date_order', '<=', self.date_to)])
        if order_detail:
            for each_order in order_detail:
                for each_order_line in each_order.lines:
                    if each_order_line.product_id.name in product_summary_dict:
                        product_qty = product_summary_dict[each_order_line.product_id.name]
                        product_qty += each_order_line.qty
                        total_sales = each_order_line.price_unit * product_qty
                        total_cost = each_order_line.product_id.standard_price*product_qty
                        total_won = total_sales - total_cost
                        try:
                            won_perecnt = (total_won * 100) / total_cost
                        except ZeroDivisionError:
                            won_perecnt = None
                        res1 = {
                            "name": each_order_line.product_id.name,
                            "sold_qty": product_qty,
                            "price_unit": each_order_line.price_unit,
                            'total_sales': total_sales,
                            "total_cost": total_cost,
                            "total_won": total_won,
                            "won_perecnt": won_perecnt,
                        }
                        data.append(res1)
                    else:
                        product_qty = each_order_line.qty
                        total_sales = each_order_line.price_unit * product_qty
                        total_cost = each_order_line.product_id.standard_price * product_qty
                        total_won = total_sales - total_cost
                        try:
                            won_perecnt = (total_won * 100) / total_cost
                        except ZeroDivisionError:
                            won_perecnt = None
                        res2 = {
                            "name": each_order_line.product_id.name,
                            "sold_qty": product_qty,
                            "price_unit": each_order_line.price_unit,
                            'total_sales': total_sales,
                            "total_cost": total_cost,
                            "total_won": total_won,
                            "won_perecnt": won_perecnt,
                        }
                        data.append(res2)
                    product_summary_dict[each_order_line.product_id.name] = product_qty;
    return data if data else {}