Skip to Content
Menu
This question has been flagged
1 Reply
5816 Views

I have two fields such as date_of_birth and age.

date_of_birth = fields.Date(string='Date of Birth')
age = fields.Char(string='Age', compute='_compute_age')

@api.depends('date_of_birth') def _compute_age(self):
    for rec in self:
        dt = rec.date_of_birth
        d2 = date.today()
        d1 = datetime.strptime(dt, "%Y-%m-%d").date()
        rd = relativedelta(d2, d1)
        rec.age = str(rd.years) + ' years ' + str(rd.months) + ' months ' + str(rd.days) + ' days'

I want to auto compute to update the age field every day. How do I do that? 

Thanks in advance

Avatar
Discard
Best Answer

Hi,

please check the codes of this module it will help you.

V11, V10

https://www.odoo.com/apps/modules/11.0/bi_birthday_reminder/

OR

V10

https://apps.odoo.com/apps/modules/10.0/birth_day_notification/

"Compute" function will run only when loading the tree view or form view.

To calculate the age in every day need to run a scheduler. This module will help you to create the scheduled cron job for the email notification. you change this and run the calculation function.

Nikhilkrishnan

Thank you

Avatar
Discard
Author

Thank you :)

You said, `"Compute" function will run only when loading the tree view or form view.`

In my case, the compute method doesn't trigger when the form view or tree view is open up, I don't know why but it should trigger as far as I know. It only triggers when the server is restarted and the views are loaded.

Would you please explain what did you mean by loading?

please check that the compute field is defined in the tree view and the form.

in xml view please add the view for age and date_of_birth fields. then try again, the compute function automatically trigger.

Related Posts Replies Views Activity
1
Aug 23
14271
2
Feb 24
13200
1
Dec 22
3863
2
Dec 22
12725
2
Jun 22
4688