Odoo IndexError: list assignment index out of range

Odoo IndexError: list assignment index out of range

我刚刚创建了一个模块。添加值后得到问题 IndexError: list assignment index out of range。如何修复它。请重新编辑代码。 这是我的代码:

class calculator(osv.osv):
    _name = 'calculator.calculator'
    def get_total(self, cr, uid, ids, field_name, arg, context):
            res = []
            perfos = self.browse(cr, uid, ids, context)
            for perfo in perfos:
                res[perfo.id] = perfo.p + perfo.b
            return res
    _columns = {
        'p':fields.selection(((1,'Outstanding'), (2,'Well Above Expectations'), (3,'As Expected'), (4,'Below Expectations'), (5,'VeryPoor'), (0,'N/A')),'title'),
        'b':fields.selection(((1,'Outstanding'), (2,'Well Above Expectations'), (3,'As Expected'), (4,'Below Expectations'), (5,'Very Poor'), (0,'N/A')),'title'),
        'total' : fields.function(get_total, method=True, string='Total Mark'),
    }

您需要 return 功能字段的字典。您将 res 定义为列表并尝试分配为字典。 res[perfo.id] 被视为列表,在 res 列表中找不到索引值 perfo.id。这就是错误所说的。现在代码将是

class calculator(osv.osv):
    _name = 'calculator.calculator'

    def get_total(self, cr, uid, ids, field_name, arg, context):
        res = {}
        for perfos in self.browse(cr, uid, ids, context):
            res[perfos.id] = perfos.p + perfos.b
        return res

    _columns = {
        'p':fields.selection(((1,'Outstanding'), (2,'Well Above Expectations'), (3,'As Expected'), (4,'Below Expectations'), (5,'VeryPoor'), (0,'N/A')),'title'),
        'b':fields.selection(((1,'Outstanding'), (2,'Well Above Expectations'), (3,'As Expected'), (4,'Below Expectations'), (5,'Very Poor'), (0,'N/A')),'title'),
        'total' : fields.function(get_total, method=True, string='Total Mark'),
    }

对于上面的代码,您可能会遇到此 js 错误 Error: [_.sprintf] expecting number but found string

我不明白将两个选择字段相加的意义。密钥将以 unicode 格式像 1+1 一样相加。

以下代码将向您介绍功能字段的基本概念。

class calculator(osv.osv):
    _name = 'calculator.calculator'

    def get_total(self, cr, uid, ids, field_name, arg, context):
        res = {}
        for perfos in self.browse(cr, uid, ids, context):
            res[perfos.id] = perfos.p + perfos.b
        return res

    _columns = {
        'p':fields.integer('A'),
        'b':fields.integer('B'),
        'total' : fields.function(get_total, method=True, string='Total Mark'),
        }

您可能需要看看

Function field is not working in OpenERP