1
votes

I have created a customized module i openerp with following .py file . Please help me to add first and second to get third using button object click function.

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

1 Answers

1
votes

For your code you can do 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

or you can make third fields as functional field and get value directly there without click on button

_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

you can not use this type return return {'value':{'third': sum }} in button clicl event it only work in onchange method, like you set onchange on fiels second so when you enter value and press tab it onchange fire set value in field third.

hope this help