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

Hello,

I'm trying to achieve a custom functionality in CRM, showing amount of days the opportunity is on the current stage. The field is working perfectly( field name: days_difference) when store=False is set.

But then, I cannot do any grouping or filtering, using this custom field, what is a goal of my project.

I have tried to set store=True and adding @api.onchange or @api.depends and use additional, not stored field (days_difference_ref, with the same calculated formula) to invoke stored field recalculation every time, the other field changes, but probably I cannot understand the logic of updating a stored field value correctly.

My code:

from datetime import datetime

from odoo import _, api, exceptions, fields, models, time


class CrmLead(models.Model):
_inherit = "crm.lead"

days_difference = fields.Integer(compute='_compute_difference', store=True)
days_difference_ref = fields.Integer(compute='_compute_difference_ref')

@api.model
def _compute_difference_ref(self):
for rec in self:
rec.days_difference_ref = (datetime.today()- rec.date_last_stage_update).days


@api.onchange('days_difference_ref')
def _compute_difference(self):
for rec in self:
rec.write({'days_difference' : days_difference_ref})


I have tried write, update without success. Apologize if it is obvious, I have just started learning and after spending 3 days on google, forum and other available sources, I've gave up.

Any hints welcome!

Avatar
Discard
Best Answer

hello,

the stored computed field changes depends on other stored fields, in this case the the time is not field and it can't be depends in the computed method

you can use work around,

you  can use stored field  days_difference and compute it 

and add cron job, in this cron call the compute method of this field on all record set to recalculate this field again

you can run it every day

thanks

Avatar
Discard