Skip to Content
Menú
This question has been flagged
1 Respondre
6692 Vistes

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
Descartar
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
Descartar
Autor

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 Respostes Vistes Activitat
1
d’ag. 23
16104
2
de febr. 24
15605
1
de des. 22
5035
2
de des. 22
14529
2
de juny 22
6400