Odoo IndexError:列表分配索引超出范围 [英] Odoo IndexError: list assignment index out of range
问题描述
我只是创建一个模块.添加值后出现问题IndexError:列表分配索引超出范围.如何解决.请重新编辑代码. 这是我的代码:
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'),
}
推荐答案
您需要返回功能字段字典的字典.您将res
定义为列表,并尝试指定为字典. res[perfo.id]
被视为列表,而在res
列表中找不到索引值perfo.id
.那就是错误的意思.现在的代码将是
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'),
}
对于上面的代码,您可能会遇到此js错误Error: [_.sprintf] expecting number but found string
For the above code you probably get this js error Error: [_.sprintf] expecting number but found string
我不明白加起来两个选择字段的意义.密钥将以unicode格式加起来像1 + 1.
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'),
}
您可能需要查看 如何为以下项设置存储触发器在Odoo 8中计算字段?
这篇关于Odoo IndexError:列表分配索引超出范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!