[Odoo][v10] 表单视图中的 cron 中的 IndexError 一切正常

[Odoo][v10] IndexError in cron in formview everything is ok

我有两种相同的方法,一种在表单视图中用于记录,另一种用于 cron。

当我在表单视图中执行 运行 操作时一切正常,我可以获得值 :

self.deadline = deadlines[0][0] 

但是当我在 cron 中 运行 时:

emp.debug = deadlines[0][0]

我有 IndexError: 列表索引超出范围 但是 emp.debug = deadlines 有效

完整代码:

class UserProfile(models.Model):
    _name = 'users.profile'

    user_id = fields.Many2one(related='project_id.user_id', string='User')
    partner_id = fields.Many2one('res.partner', 'Partner')
    follower_id = fields.Many2one('mail.followers', 'Follower')
    project_id = fields.Many2one('project.project', 'Project')
    # project_start_date  #   TODO    max date from deadlines
    project_active = fields.Boolean(related='project_id.active', string='Project active')
    project_percent = fields.Float(related='project_id.x_project_percent', string='Project percent')
    project_money = fields.Float(related='project_id.x_project_money_share')
    # project_money_paid = fields.Char(related='project_id.x_paid_debug')
    project_sale = fields.Many2one(related='project_id.x_sales_id', string='Sales')
    deadline = fields.Many2one('project.project.deadlines', 'Deadline')
    deadline_date = fields.Datetime(related='deadline.end_date')
    debug = fields.Text()

    def get_closest_date(self): # In form view
        find_deadlines = self.env["project.project.deadlines"].search([('project_id', '=', self.project_id.id)])
        deadlines = []
        for record in find_deadlines:
            datetime_without_tz = datetime.datetime.strptime(record.end_date, "%Y-%m-%d %H:%M:%S")
            record_id = record.id
            delta = datetime_without_tz - datetime.datetime.now()
            delta_in_seconds = int(delta.total_seconds())
            if delta_in_seconds > 0:
                deadlines.append((record_id, delta_in_seconds))
        deadlines.sort(key=itemgetter(1))
        self.deadline = deadlines[0][0] # No indexError i can get value
        self.debug = self.env["users.profile"].search([])[0].project_id.id

    @api.model
    def get_closest_date2(self):    # For cron
        emp_details_all = self.env["users.profile"].search([])
        for emp in emp_details_all:
            find_deadlines = self.env["project.project.deadlines"].search([('project_id', '=', emp.project_id.id)])
            deadlines = []
            for record in find_deadlines:
                datetime_without_tz = datetime.datetime.strptime(record.end_date, "%Y-%m-%d %H:%M:%S")
                record_id = record.id
                delta = datetime_without_tz - datetime.datetime.now()
                delta_in_seconds = int(delta.total_seconds())
                if delta_in_seconds > 0:
                    deadlines.append((record_id, delta_in_seconds))
            deadlines.sort(key=itemgetter(1))
            emp.debug = deadlines[0][0]     # IndexError

cron 方法中,您循环遍历所有用户配置文件,因此错误消息 IndexError 似乎是针对不同的记录显示的。

在尝试获取某些值之前检查 get_closest_date2 方法中的 deadlines 变量。