0

i am getting issue when applying compute method(_compute_days) in integer field(days) for odoo 13.

error is:-

lead.age.audit(,).days

my code is below:-

class crm_lead_age(models.Model):
    _inherit = 'crm.lead'
    
    age_audit = fields.One2many('lead.age.audit', 'lead_id', index=True, store=True)

class crm_lead_age_audit(models.Model):
    _name = 'lead.age.audit'
    
    lead_id = fields.Many2one('crm.lead')
    stage_id = fields.Many2one('crm.stage')
    date_in = fields.Date()
    date_out = fields.Date()
    days = fields.Integer(compute='_compute_days', store=True)
    
    @api.depends('date_in', 'date_out')
    def _compute_days(self):
        for res in self:
            if res.date_in and res.date_out:
                res.days = (res.date_out - res.date_in).days

Thanks in advance.

kerbrose
  • 1,095
  • 2
  • 11
  • 22
Pawan Kumar Sharma
  • 1,168
  • 8
  • 30

2 Answers2

2

Issue solved by this code:

@api.depends('date_in', 'date_out')
def _compute_days(self):
     self.days = 0
     for res in self:
        if res.date_in and res.date_out:
            res.days = (res.date_out - res.date_in).days
Pawan Kumar Sharma
  • 1,168
  • 8
  • 30
  • 1
    this is not the solution for sure. assigning `self.days = 0` would create a new variable days to the `recordset` object not the record item. could you please try the code again without self.days line – kerbrose Oct 07 '20 at 10:34
  • 1
    I too faced the same issue when i migrate my module from 12 to 13, but, no need to give self.days=0, instead dummy1=res.days, if you assign this value to any dummy variable also its working. – Krishh Dec 06 '20 at 15:03
0

Try this

@api.depends('date_in', 'date_out')
    def _compute_days(self):
        for res in self:
            if res.date_in and res.date_out:
                d1=datetime.strptime(str(self.date_in),'%Y-%m-%d') 
                d2=datetime.strptime(str(self.date_out),'%Y-%m-%d')
                d3=d2-d1
                res.days=str(d3.days)
Neural
  • 376
  • 3
  • 12