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:
- CRM
- e-Commerce
- Accounting
- Inventory
- PoS
- Project management
- MRP
This question has been flagged
2
Replies
1438
Views
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 = ...
Enjoying the discussion? Don't just read, join in!
Create an account today to enjoy exclusive features and engage with our awesome community!
Sign upRelated Posts | Replies | Views | Activity | |
---|---|---|---|---|
|
1
Nov 24
|
1482 | ||
|
1
Nov 24
|
1188 | ||
|
2
Sep 24
|
1046 | ||
Update stock quantity
Solved
|
|
1
Aug 24
|
2450 | |
|
3
Aug 24
|
2684 |
watch this to understand why singleton error appears: https://www.youtube.com/watch?v=Rv44nFVn_5U