2016-04-06 52 views
1

我在发票模块中创建一个函数。你应该做的是让这个功能的产品线是平等的发票,并返回每个产品的金额。这被下面的图像中更好地解释:Odoo错误:TypeError:'int'对象不可迭代

enter image description here

例如,产品“Aceite Hidraulico”出现3次在发票上。然后函数应该返回一个3这个产品。

好了,这是我在我的account.invoice.py功能:

total_quantity = fields.Integer(compute='count_invoice_invoice', string = 'Cantidad Productos', readonly = True, store=True) 

@api.one 
@api.depends('invoice_line_ids.product_id') 
def count_invoice_invoice(self): 
    res = {} 
    counter = 0 
    for po in self.invoice_line_ids: 
     for product in po.product_id.id: 
      counter += 1 
      res [self.total_quantity] = counter 
    return res 

对于功能区分一个产品和另一个之间,我用的是id字段product.product模型。但是当我尝试创建发票时,Odoo向我显示一条消息int类型字段不能被迭代。这是一个问题,因为用于区分产品的id字段是整数。

我该怎么办?

这是显示Odoo消息:

for product in po.product_id.id: 

TypeError: int object is not iterable

非常感谢您的帮助和花费的时间。我很欣赏任何建议或建议,以获得您想要做的事情。

+0

事情是,如果您将其存储在整数字段中,则每个发票只有1个计数。它不会帮助你,你必须做的是为报告制作一个功能。 无论如何,你不需要第二次迭代,第一次你已经在线。您必须做的是将产品保存在例如字典中,并且如果密钥已经存在,则将该金额添加到该字典中。你仍然不能保存它作为一个领域。 – dccdany

回答

0

其实你有for循环的问题。 你正在尝试整数循环。 po.product_id.id是整数所以我觉得没有必要使用该for循环甚至认为,如果你想使用的循环使用是这样的: 产品在[po.product_id.id]

感谢

1

似乎您试图环路上一个整数。如果您想增加计数器,请尝试在对象上循环。 对于防爆:

for po in self.invoice_line_ids: 
    for product in po.product_id: 
     counter += 1 
     res [self.total_quantity] = counter 
return res 

通过这种方式,您可以通过对象循环,如果你需要得到每个产品ID可以通过得到它的product.id内循环。