Odoo IndexError: list assignment index out of range

你离开我真会死。 提交于 2019-12-24 07:26:58

问题


I just create a module. After adding values and got the problem IndexError: list assignment index out of range. How to fix it. Re Edit code please. Here is my code:

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:


You need to return a dictionary of dictionary for functional fields. You defined res as list and tried to assign as the dictionary. res[perfo.id] is considered as list and index value perfo.id is not found in the res list. That is what the error says. Now the code will be

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

For the above code you probably get this js error Error: [_.sprintf] expecting number but found string

I don't get the point adding up two selection fields. The key will get added up like 1+1 in a unicode format.

This following code will give you the basic idea of functional fields.

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

You may need to look at How to set store trigger for computed fields in Odoo 8?

Function field is not working in OpenERP



来源:https://stackoverflow.com/questions/29881228/odoo-indexerror-list-assignment-index-out-of-range

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!