代码之家  ›  专栏  ›  技术社区  ›  Pointer

预期独生子女奥多9

  •  2
  • Pointer  · 技术社区  · 8 年前

    在树状视图中输入2行或更多新行,然后单击save get error

      raise ValueError("Expected singleton: %s" % self)
    ValueError: Expected singleton: my.model(2116, 2117)
    

    我的源代码:

    @api.depends('start', 'finish','stop')
    def total_fun(self): 
        time1 = datetime.strptime(self.start, "%Y-%m-%d %H:%M:%S")
        time2 = datetime.strptime(self.finish, "%Y-%m-%d %H:%M:%S")
        self.total = round(((time2 - time1).seconds / float(60*60) - self.stop))
    
    2 回复  |  直到 8 年前
        1
  •  4
  •   Dachi Darchiashvili Mariusz Mizgier    8 年前

    错误消息显示-> expected singleton 这意味着:您使用的是记录集而不是记录。

    修复此用途

    for rec in self:
    

    在函数的开头,然后使用 rec self

        2
  •  1
  •   Charif DZ    8 年前

    正如您在错误消息中看到的那样 Expected singleton: my.model(2116, 2117)

    self.getSomeField 在这里,奥多将困惑于你想要从哪个记录中获得价值。

    现在,如何告诉奥多确保总是有一个记录是通过添加 @api.one 装饰方法。但不建议这样做,因为在您的例子中,odoo有两条记录,所以他将循环并调用每个记录的方法,并传递一个只有该记录的记录集。假设您执行搜索或与数据库的任何通信。

    所以不要使用 只有当您确信自己在做什么时,因为您可以进行10000个方法调用并与数据库交互。

    就像这个例子使用 :

       # every call to this method you will execute a search.
       self.env['some.model'].search([('m2o_id' , '=', self.id)]
    

      # one query for all record with one call to the method
      result = self.env['some.model'].search([('m2o_id' , 'in', self.ids)]
      for rec in self:
          # use result here 
      # or here ..