Přejít na obsah
Menu
You need to be registered to interact with the community.
This question has been flagged
1 Odpovědět
6739 Zobrazení

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
Zrušit
Nejlepší odpověď

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
Zrušit
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 Odpovědi Zobrazení Aktivita
1
srp 23
16198
2
úno 24
15652
1
pro 22
5080
2
pro 22
14592
2
čvn 22
6483