@api.depends('join_date', 'todays_time')
def compute_exprience(self):
for rec in self:
if rec.join_date and rec.todays_time:
r1 = datetime.datetime.strptime(str(self.todays_time), '%Y-%m-%d')
r2 = datetime.datetime.strptime(str(self.join_date), '%Y-%m-%d')
rec.Experience = (r1.year - r2.year)
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ờ
Hi
amina lifam,
Try,
experience = fields.Char(string='Experience', compute='compute_experience')
@api.depends('join_date')
def compute_experience(self):
for rec in self:
if rec.join_date:
join_date = rec.join_date
current_date = fields.Date.today()
years_diff = current_date.year - join_date.year
months_diff = current_date.month - join_date.month
if current_date.day < join_date.day:
months_diff -= 1
if months_diff < 0:
years_diff -= 1
months_diff += 12
experience_str = '{} years {} months'.format(years_diff, months_diff)
rec.experience = experience_str
else:
rec.experience = 'N/A'
Output:
Hope it helps,
Kiran K
Try this:
from datetime import datetime
joining_date = datetime.strptime("join_date", "%Y-%m-%d")
today = datetime.today()
# Calculate total months and years
total_months = (today.year - joining_date.year) * 12 + (today.month - joining_date.month)
years, months = divmod(total_months, 12)
Then put it in rec.experience.....
Note: Don't use capital letter in fields like rec.Experience...
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 6 25
|
1798 | ||
|
3
thg 7 25
|
3396 | ||
|
1
thg 5 25
|
1542 | ||
|
1
thg 5 25
|
1803 | ||
|
4
thg 5 25
|
2927 |