field.function - KeyError:40

时间:2014-01-29 06:48:20

标签: openerp

当我更新它抛出KeyError:40

的值时,创建了一个列'line_no'

我的代码:

def _get_line_no(self, cr, uid, ids, line_no, arg, context=None):
    res = {}
    for record in self.browse(cr, uid, ids, context=context):                     
        nextno =0
        no = record.next_line_no        
        next_no = nextno + no
        total =+ next_no
        res={
                  'next_line_no':next_no,
                  'line_no': total
        } 
    return res

_columns = {
 'line_no':fields.function(_get_line_no,string='Line No',type='integer'),
 'next_line_no':fields.integer(' Next Line No'),
  }
_defaults = {
    'next_line_no':1  
  }

引发错误:KeyError:40

我如何解决?

2 个答案:

答案 0 :(得分:2)

next_line_no是一个数据库字段,因此它不会影响动态方式。

你需要修改这种方式,

def _get_line_no(self, cr, uid, ids, line_no, arg, context=None):
    res = {}
    for record in self.browse(cr, uid, ids, context=context):                     
        nextno =0
        no = record.next_line_no        
        next_no = nextno + no
        total += next_no
        res[record.id]={
                  'next_line_no':next_no,
                  'line_no': total
        } 
    return res

_columns = {
 'line_no':fields.function(_get_line_no,string='Line No',type='integer', multi="lineno"),
 'next_line_no': function(_get_line_no,string='Next Line No',type='integer', multi="lineno", store=True),
  }
_defaults = {
    'next_line_no':1  
  }

我希望这会让你感到高兴。

答案 1 :(得分:1)

按列更改列,

_columns = {
    'line_no':fields.function(_get_line_no,string='Line No',type='integer', multi="line"),
    'next_line_no':fields.function(_get_line_no, type='integer', string='next line number' ,multi="line"),
}

和你的方法一样,

def _get_line_no(self, cr, uid, ids, field_names, args, context=None):
    res = {}
    for record in self.browse(cr, uid, ids, context=context):                     
        nextno =0
        no = record.next_line_no        
        next_no = nextno + no
        total += next_no
        res[record.id]={
                  'next_line_no':next_no,
                  'line_no': total
        } 
    return res

这样可行。