2014-01-13 27 views
1

我已經創建了一個自定義的模塊我openerp與下面的.py文件。請幫我添加第一個和第二個獲得第三個使用按鈕對象點擊功能。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"/> 

回答

1

爲您的代碼,你可以做onething

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:'), '第二':fields.integer( '第二號:'), '第三':fields.function(_sum,TYPE = 「浮動」,店內= 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 

不能使用這個類型返回返回{'value':{'third':sum}}在按鈕clicl事件中它只能在onchange方法中使用,就像你在fiels秒上設置onchange一樣,所以當你輸入值並按下tab鍵改變fire設置值在第三場。

希望得到這個幫助

+0

非常感謝您的完整信息。我想明確地使用按鈕點擊事件。再次感謝您的快速響應。 – user3153567

+0

歡迎朋友 – user1576199

+0

在最後添加括號並執行。它工作正常。 self.write(cr,uid,ids,{'third':sum}) – user3153567