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!