Skip to Content
Menu
This question has been flagged
3 Replies
3138 Views

I'm trying to assigned value to 'hours' toward a compute fuction but this fall in an error. here is the code:

class fly_personne(models.Model):

    _name = 'fly.personne'

    _inherit = 'fly.pers'

    _description = "personne"


    def calculTemps(self):

        sql1 = "select sum(b.hours) from fly_ligne_personne a, fly_vol b where a.vol_id = b.id and a.pilote_id = '%s' GROUP BY a.pilote_id ;" % self.id

        self.env.cr.execute(sql1)

        rec.update({'hours': self.env.cr.fetchall()})


    hours = fields.Char('Temps de vol', compute='calculTemps')


2) also is it possible to return more than one value for the sql  ?

ex: doing something like:

...

sql1 = "select sum(b.hours), sum(b.minutes), a.pers_id from ...

        self.env.cr.execute(sql1)

        rec.update({'hours': self.env.cr.fetchall()})

    rec.update({'minutes': ?????})
    rec.update({'pers_id': ????})

 hours = fields.Char('Temps de vol', compute='calculTemps')

 minutes= fields.Char('mn de vol', compute='calculTemps')

 pers_id= fields.Char('id pers', compute='calculTemps')

Avatar
Discard
Author Best Answer

ok, i reveiw my code , but it still fall in an error.

code:

...

    def calculTemps(self):

        total = 0

        for rec in self:

            self.env.cr.execute("""select sum(b.hours) as hours from fly_ligne_personne a, fly_vol b where a.vol_id = b.id and a.personne_id = '%s' GROUP BY a.personne_id """, str(self.id))

            results = self.env.cr.fetchall()

            total = results[0]

            rec.update({'hours': total})

    hours = fields.Char('Temps de vol', compute='calculTemps')


Avatar
Discard
Best Answer

While fetching the result of psql query using fetchall() returns results in a [], and dictfetchall() returns results with key and values as a {}. So you should change the code accordingly. to compute one more field with same functions its possible and you have to add depends decorator with function.

eg:

@api.depends('hours', 'minutes', ..)
Avatar
Discard