我已经使用以下.py文件创建了一个自定义模块i openerp。请帮我添加第一个和第二个以使用按钮对象点击功能获得第三个。
from osv import osv
from osv import fields
class test_base(osv.osv):
_name='test.base'
_columns={
'first':fields.integer('First No:'),
'second':fields.integer('Second No:'),
'third':fields.integer('Third No:'),
}
def get_sum(self, cr, uid, ids,context=None):
# please add code here to get sum of 'first' and 'second' and assign to variable 'sum'
return {'value':{'third': sum }}
test_base()
XML
<button name="get_sum" string="Click on me to get sum " type="object"/>
答案 0 :(得分:1)
对于您的代码,您可以执行
def get_sum(self, cr, uid, ids,context=None):
# please add code here to get sum of 'first' and 'second' and assign to variable 'sum'
sum = 0.0
for data in self.browse(cr, uid, ids, context=context):
sum += data.first + data.second
self.write(cr, uid, ids, {'third': sum}
return True
或者您可以将第三个字段作为功能字段直接获取值,而无需单击按钮
_columns = { 'first':fields.integer('First No:'), 'second':fields.integer('Second No:'), 'third':fields.function(_sum,type =“float”,store = True) }
def get_sum(self, cr, uid, ids,context=None):
res = {}
# please add code here to get sum of 'first' and 'second' and assign to variable 'sum'
sum = 0.0
for data in self.browse(cr, uid, ids, context=context):
sum += data.first + data.second
res[data.id] = sum
return res
你不能使用这种类型返回 return {'value':{'third':sum}} 在按钮clicl事件中它只能在onchange方法中工作,就像你在fiels第二次设置onchange所以输入数值并按Tab键时,在第三个字段中更改火灾设定值。
希望这个帮助