Sum times odoo 9

229 views Asked by At

When use compute in tree view sum is not visible. When use onChange sum is visible any solution how fix it. I need compute after insert data from .csv automaticly populate time_total fields.

Example:

Example

Source:

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)  

SHOW SUM IN TREE VIEW WHEN MANUAL CHANGE VALUE IN FORM VIEW

 @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)
1

There are 1 answers

0
Emipro Technologies Pvt. Ltd. On BEST ANSWER

Just do one change in that,

Store that field in database and it will show you the sum of that field.

total_time = fields.Float(string='Total minutes', placeholder="Total", compute='onchange_time', store=True)

And then remove onchange and 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)

There is reason behind that scenario is, group by operation required field in database because odoo frameworks prepared query for group by and then get result back from the database. So if the field is not there in database then how it can show you result.