首页 > 解决方案 > 如何解决错误“IndexError:列表索引超出范围”?

问题描述

我正在使用 Odoo 11 并且已经安装了 openHRMS 模块,但是每次按下仪表板菜单时,此方法都会出现错误:

IndexError:列表索引超出范围

Ps:我试图在bitnami VM上安装这个模块并且它没有任何问题但是当我试图在ubuntu 18.04上安装它时它显示错误

关于如何解决它的任何想法?

 def join_resign_trends(self):
    cr = self._cr
    month_list = []
    join_trend = []
    resign_trend = []
    for i in range(11, -1, -1):
        last_month = datetime.now() - relativedelta(months=i)
        text = format(last_month, '%B %Y')
        month_list.append(text)
    for month in month_list:
        vals = {
            'l_month': month,
            'count': 0
        }
        join_trend.append(vals)
    for month in month_list:
        vals = {
            'l_month': month,
            'count': 0
        }
        resign_trend.append(vals)
    cr.execute('''select to_char(joining_date, 'Month YYYY') as l_month, count(id) from hr_employee 
    WHERE joining_date BETWEEN CURRENT_DATE - INTERVAL '12 months'
    AND CURRENT_DATE + interval '1 month - 1 day'
    group by l_month;''')
    join_data = cr.fetchall()
    cr.execute('''select to_char(resign_date, 'Month YYYY') as l_month, count(id) from hr_employee 
    WHERE resign_date BETWEEN CURRENT_DATE - INTERVAL '12 months'
    AND CURRENT_DATE + interval '1 month - 1 day'
    group by l_month;''')
    resign_data = cr.fetchall()

    for line in join_data:
        match = list(filter(lambda d: d['l_month'].replace(' ', '') == line[0].replace(' ', ''), join_trend))
        match[0]['count'] = line[1]
    for line in resign_data:
        match = list(filter(lambda d: d['l_month'].replace(' ', '') == line[0].replace(' ', ''), resign_trend))
        match[0]['count'] = line[1]
    for join in join_trend:
        join['l_month'] = join['l_month'].split(' ')[:1][0].strip()[:3]
    for resign in resign_trend:
        resign['l_month'] = resign['l_month'].split(' ')[:1][0].strip()[:3]
    graph_result = [{
        'name': 'Join',
        'values': join_trend
    }, {
        'name': 'Resign',
        'values': resign_trend
    }]
    return graph_result

 def get_attrition_rate(self):
    month_attrition = []
    monthly_join_resign = self.join_resign_trends()
    month_join = monthly_join_resign[0]['values']
    month_resign = monthly_join_resign[1]['values']
    sql = """
    SELECT (date_trunc('month', CURRENT_DATE))::date - interval '1' month * s.a AS month_start 
    FROM generate_series(0,11,1) AS s(a);"""
    self._cr.execute(sql)
    month_start_list = self._cr.fetchall()
    for month_date in month_start_list:
        self._cr.execute("""select count(id), to_char(date '%s', 'Month YYYY') as l_month from hr_employee 
        where resign_date> date '%s' or resign_date is null and joining_date < date '%s'
        """ % (month_date[0], month_date[0], month_date[0],))
        month_emp = self._cr.fetchone()
        # month_emp = (month_emp[0], month_emp[1].split(' ')[:1][0].strip()[:3])
        match_join = list(filter(lambda d: d['l_month'] == month_emp[1].split(' ')[:1][0].strip()[:3], month_join))[0]['count']
        match_resign = list(filter(lambda d: d['l_month'] == month_emp[1].split(' ')[:1][0].strip()[:3], month_resign))[0]['count']
        month_avg = (month_emp[0]+match_join-match_resign+month_emp[0])/2
        attrition_rate = (match_resign/month_avg)*100 if month_avg != 0 else 0
        vals = {
            # 'month': month_emp[1].split(' ')[:1][0].strip()[:3] + ' ' + month_emp[1].split(' ')[-1:][0],
            'month': month_emp[1].split(' ')[:1][0].strip()[:3],
            'attrition_rate': round(float(attrition_rate), 2)
        }
        month_attrition.append(vals)
    return month_attrition

追溯

   Traceback (most recent call last):
   File "/opt/openhrms/odoo/http.py", line 651, in _handle_exception
return super(JsonRequest, self)._handle_exception(exception)
  File "/opt/openhrms/odoo/http.py", line 310, in _handle_exception
raise pycompat.reraise(type(exception), exception, sys.exc_info()[2])
  File "/opt/openhrms/odoo/tools/pycompat.py", line 87, in reraise
raise value
  File "/opt/openhrms/odoo/http.py", line 693, in dispatch
result = self._call_function(**self.params)
  File "/opt/openhrms/odoo/http.py", line 342, in _call_function
return checked_call(self.db, *args, **kwargs)
  File "/opt/openhrms/odoo/service/model.py", line 97, in wrapper
return f(dbname, *args, **kwargs)
  File "/opt/openhrms/odoo/http.py", line 335, in checked_call
result = self.endpoint(*a, **kw)
  File "/opt/openhrms/odoo/http.py", line 937, in __call__
return self.method(*args, **kw)
  File "/opt/openhrms/odoo/http.py", line 515, in response_wrap
response = f(*args, **kw)
 File "/opt/openhrms/addons/web/controllers/main.py", line 934, in call_kw
return self._call_kw(model, method, args, kwargs)
 File "/opt/openhrms/addons/web/controllers/main.py", line 926, in _call_kw
return call_kw(request.env[model], method, args, kwargs)
 File "/opt/openhrms/odoo/api.py", line 687, in call_kw
return call_kw_model(method, model, args, kwargs)
 File "/opt/openhrms/odoo/api.py", line 672, in call_kw_model
result = method(recs, *args, **kwargs)
 File "/opt/openhrms/openhrms/hrms_dashboard/models/hrms_dashboard.py", line 384, in get_attrition_rate
monthly_join_resign = self.join_resign_trends()
 File "/opt/openhrms/openhrms/hrms_dashboard/models/hrms_dashboard.py", line 364, in join_resign_trends
match[0]['count'] = line[1]
IndexError: list index out of range

标签: python-3.xodoo-11

解决方案


推荐阅读