-1

我正在尝试计算工人的销售额,但在使用下面提到的代码时出现以下错误:

TypeError: 'bool' 类型的对象没有 len()

类运动类型(模型。模型):

   _name = 'project_rc.movement_type'

   _rec_name = 'movement_type'

type_movement = fields.Selection ([('purchase', 'Purchase'), ('sale', 'Sale'), ('merma', 'Merma')], string = "运动类型", required = True)

类工人(models.Model):

  _name = 'project_rc.worker'

  _rec_name = 'name'

sales_counter = fields.Integer (string = "Sales made", compute = "get_sales_realized", store = True)

@api.depends('move_type_ids')

  def get_sales_realized (self):

    for rec in self:

        rec.count_sale = len (rec.move_type_ids.mov_type == 'sale')
4

1 回答 1

1

我不熟悉您使用的任何框架,但如果您查看您收到的错误,您会发现它是正确的。

在第 3 行,您编写rec.move_type_ids.mov_type == 'sale'. 不管是什么rec.move_type_ids.mov_type,当您将它与 比较时==,答案将是TrueFalse。取布尔值 (t/f) 的长度是没有意义的。

从上下文来看,我猜这rec.move_type_ids是一个对象列表,您想弄清楚其中有多少具有mov_type等于'sale'. 如果是这种情况,那么您可以使用 for 循环轻松做到这一点:

sales = []
for thing in rec.move_type_ids:
    if thing.type == 'sale':
        sales.append(thing)
rec.count_sale = len(sales)

如果你想变得更漂亮一点,你可以用一个filter函数来做到这一点:

rec.count_sale = len(filter(lambda x: x.mov_type == 'sale', rec.move_type_ids))
于 2020-02-27T19:33:00.540 回答