Hi, i encountered raise ValueError("Expected singleton: %s" % self) when computing age and working time
How to fix it
#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'
Odoo is the world's easiest all-in-one management software.
 It includes hundreds of business apps:
- 客户关系管理
- e-Commerce
- 会计
- 库存
- PoS
- Project
- MRP
此问题已终结
            
                2
                
                    回复
                
            
        
        
            
                2986
                
                    查看
                
            
        
    Hi Denny,
Please try to use a for loop in the function. 
Like:
#computing age
@api.depends('birth_date')
def _count_age(self):
for record in self:
if record.date_birth is not False:
record.age = (datetime.today().date() - datetime.strptime(str(record.date_birth),'%Y-%m-%d').date()) // timedelta(days=365)
else:
record.age = 0.0
#computing working time
@api.depends('start_work')
# @api.multi
def _long_work(self):
for record in self:
if record.start_work:
years = relativedelta(date.today(), record.start_work).years
months = relativedelta(date.today(), record.start_work).months
day = relativedelta(date.today(), record.start_work).days
record.work_long = str(int(years)) + ' Year ' + str(int(months)) + ' Month ' + str(day) + ' Day'
else:
record.work_long = '0 Year 0 Month 0 Day'
Hope it will help you.
Thanks and Vote up please.
Hi Denny,
Singleton error occurs when you call self.some_field where self is a collection of records. Instead, you should loop through the records like this;
for rec in self:
rec.some_field = ...
| 相关帖文 | 回复 | 查看 | 活动 | |
|---|---|---|---|---|
|  | 3 9月 25  | 3362 | ||
|  | 0 8月 25  | 874 | ||
|  | 1 8月 25  | 3129 | ||
|  | 2 7月 25  | 8763 | ||
|  | 2 7月 25  | 5116 | 
 
                        
watch this to understand why singleton error appears: https://www.youtube.com/watch?v=Rv44nFVn_5U