-1

i encountered raise when trying to compute age and working time ValueError("Expected singleton: %s" % self)) ValueError: Expected singleton: restaurant.karyawan(1, 2)

#computing age
 @api.depends('tanggal_lahir')
    def _hitung_usia(self):
        if self.tanggal_lahir is not False:
            self.usia = (datetime.today().date() - datetime.strptime(str(self.tanggal_lahir),'%Y-%m-%d').date()) // timedelta(days=365)

#computing working time
    @api.depends('mulai_bekerja')
    # @api.multi
    def _lama_bekerja(self):
        if self.mulai_bekerja:
            years = relativedelta(date.today(), self.mulai_bekerja).years
            months = relativedelta(date.today(), self.mulai_bekerja).months
            day = relativedelta(date.today(), self.mulai_bekerja).days
        self.lama_bekerja = str(int(years)) + ' Tahun ' + str(int(months)) + ' Bulan ' + str(day) + ' Hari'

how to resolve it ?

Denny
  • 3
  • 3

1 Answers1

0

Computed methods may get called in tree views if fields are present or still needed to be computed for any dependency.

This means your self will contain N instances of your model instead of one. Ensure your logic supports multi instances by iterating over them.

@api.multi
def _compute_fun(self):
  for record in self:
     // record.field = ... logic
icra
  • 460
  • 3
  • 14