Hi All ! I have a query please. I have used a function to calculate the age of an employee. it is based on on change. Whenever we add a date of birth, it calculates the age. Now the issue is as it is based on change of date of birth, the age is not calculating on daily basis unless we edit the date of birth field. I have also tried via depends but same results. The code is mentioned below.
employee_age = fields.Integer(string="Employee Age")
@api.onchange('date_birth')
def _onchange_birth_date(self):
"""Updates age field when birth_date is changed"""
if self.date_birth:
d1 = datetime.strptime(str(self.date_birth), "%Y-%m-%d").date()
d2 = date.today()self.employee_age = relativedelta(d2, d1).years
I have used cron job and called a function(_onchange_birth_date) inside a function but doesn't worked for me. May be I am wrong at calling.
@api.model
def dob_calculate(self):
self.search([('date_birth', '!=', fields.Date.to_string(date.today())),
]).write
({'employee_age': self.onchange_emp_dob})
P.S I am new to python.
Sorry for indentations
Regards,
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ờ
1
Trả lời
3408
Lượt xem
try this:
from datetime import datetime, timedeltabirthday = fields.Date(
string='Birthday',
required=False)
age = fields.Integer(
string='Edad',
required=False, compute='_compute_age')
@api.depends('birthday')
def _compute_age(self):
if self.birthday is not False:
self.age = (datetime.today().date() -
datetime.strptime(str(self.birthday),'%Y-%m-%d').date()) // timedelta(days=365)
or check this: https://www.odoo.com/es_ES/forum/ayuda-1/how-to-calculate-age-from-birthday-127493
Thank you Gerson,
I Will try it
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 3 15
|
4928 | ||
|
0
thg 3 19
|
2504 | ||
|
0
thg 3 15
|
4092 | ||
|
2
thg 4 24
|
1331 | ||
|
1
thg 5 22
|
2248 |
Instead of onchange, you have to do it using non stored computed field.
@niyas
How will then I use it in pivot and other filters as non-stored values are not appearing in the filters and pivot view even after adding in depends