In my module I have an entity "Person" with two fields "date_of_birth" (Date) and "age" (Integer). How can I get the age of a Person from the date of birth?
Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:
- सीआरएम
- e-Commerce
- लेखांकन
- इन्वेंटरी
- PoS
- Project
- MRP
This question has been flagged
Hi Ernesto:
Make "age" a computed field with the following settings:
Readonly = True
Stored = False
Copied = False
Depends on: date_of_birth
for record in self: if record.date_of_birth:
today = datetime.date.today() # Check if the date has passed this year if today.strftime("%m%d") >= record.date_of_birth.strftime("%m%d"):
record['age'] = today.year - record.date_of_birth.year
else: record['age'] = today.year - record.date_of_birth.year - 1
else:
record['age'] = 0
Hi Ernesto:
You can do like this:
from dateutil.relativedelta import relativedelta
age = fields.Integer(string="Age", compute="_calculate_age")
@api.depend('date_of_birth')
def _calculate_age(self):
if self.date_of_birth:
d1 = self.date_of_birth
d2 = datetime.date.today()
self.age = relativedelta(d2, d1).years
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
सित॰ 18
|
4528 | ||
|
1
जन॰ 17
|
5210 | ||
|
0
मार्च 25
|
1529 | ||
|
4
अप्रैल 24
|
174408 | ||
|
2
मार्च 24
|
2054 |