在树视图中输入2行或更多新行后,单击“保存获取错误”
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))发布于 2017-07-05 12:50:17
错误消息表示-> expected singleton,这意味着:您使用的是记录集而不是记录。
要修复此使用,请执行以下操作
for rec in self:在函数的开始,然后使用rec代替self。
发布于 2017-07-05 20:25:14
正如您在错误消息Expected singleton: my.model(2116, 2117)中看到的那样
默认情况下,在odoo中,self总是一个recordSet (意味着它可以包含多个记录)。因此,当您在这里执行self.getSomeField时,odoo将被您想从其中获取值的记录所混淆。
如果您不告诉odoo,确保当您访问属性时,如果recordSet包含多个记录,则self将始终包含一个记录,则会引发此错误。
现在,如何告诉odoo,确保始终有一个记录是通过向方法中添加@api.one装饰器来实现的。但是不建议这样做,因为在您的示例中,odoo有两个记录,因此他将循环并调用每个记录的方法,并传递一个只有该记录的recordSet。假设您执行搜索或与数据库的任何通信。
因此,仅当您确定要做什么时,才不要使用@api.one,因为您可以进行10000方法调用并与数据库交互。
就像这个使用@api.one的例子
# 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 ..https://stackoverflow.com/questions/44926593
复制相似问题