0

在 odoo 13 的整数字段(天)中应用计算方法(_compute_days)时,我遇到了问题。

错误是:-

Lead.age.audit(,).days

我的代码如下:-

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

提前致谢。

4

2 回答 2

2

此代码解决的问题:

@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
于 2020-10-04T11:29:45.620 回答
0

试试这个

@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)

于 2020-10-03T16:08:00.327 回答