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
- Kế toán
- Tồn kho
- PoS
- Project
- MRP
Câu hỏi này đã bị gắn cờ
2
Trả lời
2462
Lượt xem
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 = ...
Bạn có hứng thú với cuộc thảo luận không? Đừng chỉ đọc, hãy tham gia nhé!
Tạo tài khoản ngay hôm nay để tận hưởng các tính năng độc đáo và tham gia cộng đồng tuyệt vời của chúng tôi!
Đăng kýBài viết liên quan | Trả lời | Lượt xem | Hoạt động | |
---|---|---|---|---|
|
1
thg 7 25
|
2361 | ||
|
2
thg 7 25
|
7855 | ||
|
2
thg 7 25
|
4254 | ||
|
2
thg 7 25
|
4017 | ||
|
2
thg 6 25
|
2618 |
watch this to understand why singleton error appears: https://www.youtube.com/watch?v=Rv44nFVn_5U