Odoo IndexError:列表赋值索引超出范围

时间:2015-04-26 17:50:25

标签: openerp odoo

我只是创建一个模块。添加值后得到问题IndexError:列表赋值索引超出范围。如何解决它。请重新编辑代码。 这是我的代码:

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'),
    }

1 个答案:

答案 0 :(得分:2)

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

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'),
        }

您可能需要查看 How to set store trigger for computed fields in Odoo 8?

Function field is not working in OpenERP