Ir al contenido
Menú
Se marcó esta pregunta
1 Responder
6690 Vistas

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
Mejor respuesta

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.

Publicaciones relacionadas Respuestas Vistas Actividad
1
ago 23
16088
2
feb 24
15601
1
dic 22
5022
2
dic 22
14508
2
jun 22
6377