在树视图中使用compute时,总和不可见。当使用onChange sum时,任何解决方案都可以修复它。在插入来自.csv的数据后,我需要计算自动填充time_total字段。
示例:
来源:
class my_data(models.Model):
_name = "my.data"
_description = "My Data"
user = fields.Char(string = 'User')
date = fields.Date(string = 'Date')
start_time = fields.Datetime(string='Start', placeholder="Start", default="2016-01-01 00:00:00.624139")
finish_time = fields.Datetime(string='Finish', placeholder="Finish", default="2016-01-01 00:00:00.624139")
total_time = fields.Float(string='Total minutes', placeholder="Total", compute='onchange_time')
#total_time = fields.Float(string='Total minutes', placeholder="Total minutes")
@api.multi
@api.onchange('start_time', 'finish_time')
def onchange_time(self):
for rec in self:
time1 = datetime.strptime(rec.start_time, "%Y-%m-%d %H:%M:%S")
time2 = datetime.strptime(rec.finish_time, "%Y-%m-%d %H:%M:%S")
rec.total_time = (time2 - time1).seconds / float(60*60)
在形式视图中手动更改值时,在树视图中显示总和
@api.onchange('start_time', 'finish_time')
def onchange_time(self):
time1 = datetime.strptime(self.start_time, "%Y-%m-%d %H:%M:%S")
time2 = datetime.strptime(self.finish_time, "%Y-%m-%d %H:%M:%S")
self.total_time = (time2 - time1).seconds / float(60*60)
答案 0 :(得分:3)
只需做一处更改,
将该字段存储在数据库中,它将显示该字段的总和。
total_time = fields.Float(string='Total minutes', placeholder="Total", compute='onchange_time', store=True)
然后删除onchange和insted use depends
@api.depends('start_time', 'finish_time')
def onchange_time(self):
time1 = datetime.strptime(self.start_time, "%Y-%m-%d %H:%M:%S")
time2 = datetime.strptime(self.finish_time, "%Y-%m-%d %H:%M:%S")
self.total_time = (time2 - time1).seconds / float(60*60)
这种情况背后有理由,需要按操作分组 数据库中的字段,因为odoo框架为group by准备了查询 然后从数据库中获取结果。所以,如果该领域不存在 在数据库中然后它如何显示结果。