Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

1

how to call function inside another function

By
mihir shah
on 5/3/13, 4:23 AM 5,944 views

def confirmation(self, cr, uid, ids, context={}): ir_model_data = self.pool.get('ir.model.data')
return {

                'type': 'ir.actions.act_window',
        'view_type': 'form',
        'view_mode': 'form',
        'res_model': 'die.approval',
                        }


def action_confirm_draft(self, cr, uid, ids, context={}):
            if confirmation(self, cr, uid, ids, context={}) == True: 
                self.write(cr, uid, ids, {'state': 'sample','approved_by':uid})
                return True
            else:
                    return False

i am trying to call confirmation function in action_confirm_draft() , but is ven i call the action_confirm_draft function it gives me( NameError: global name 'confirmation' is not defined) error

5

Sandeep

--Sandeep--
6083
| 6 5 8
Delhi, India
--Sandeep--
Python OpenERP Developer http://in.linkedin.com/pub/sandeep-rana/3b/a16/786/
Sandeep
On 5/6/13, 4:50 AM

hello mihir

Use "self" to call function if both functions inside same class (model)

if calling functions of another class (model) then use self.pool.get("model.name").calling_func(cr,uid,.........)

self property of python not of openep

that's it

1

Naresh Soni (nch)

--Naresh Soni (nch)--
Tiny ERP Pvt Ltd
http://www.odoo.com
1281
| 2 2 4
India
--Naresh Soni (nch)--

Team Lead, Odoo(Formerly OpenERP) Support & OPW

Naresh Soni (nch)
On 5/3/13, 7:16 AM

Hello Mihir,

Do this:

def action_confirm_draft(self, cr, uid, ids, context={}):
        if self.confirmation(cr, uid, ids, context): 
            return self.write(cr, uid, ids, {'state': 'sample','approved_by':uid})
        return False

Thanks,

Hello Naresh if i am using the following it is throwing this error (TypeError: confirmation() got multiple values for keyword argument 'context' )

mihir shah
on 5/4/13, 4:24 AM

sorry my mistake I updated the syntax

Naresh Soni (nch)
on 5/6/13, 4:30 AM
0

Drees Far

--Drees Far--
1130
| 5 2 6
Tunis, Tunisia
--Drees Far--
Drees Far
On 8/13/15, 5:19 AM

The easiest example is of the on_change function. Here i give you an example of a call to a function.


def on_change_state_id(self, cr, uid, ids,state, project, credit, context=None): 

if state==1:

project_id = _get_project_id_by_name (self,cr, uid, project, context)

res = {'value':{'service_ids': self.get_inputs(cr, uid, ids, state, project,context=context),

}

}  

return res

else:

return False

---------------------------------------------------


def get_inputs(self, cr, uid,ids, state, project, context=None):

ret = [] 

project_id = _get_project_id_by_name (self,cr, uid, project, context

obj = self.pool.get('tes.project.service')

obj_ids = obj.search(cr, uid, [], context=context)

res = obj.read(cr, uid, obj_ids, ['id', 'int_service','ext_service','unit_service','int_service_unit','ext_service_unit','service_type','service_condition'], context)

for r in res :

inputs = {

'project_id' : project_id,

'int_services': r['int_service'],

'ext_services': r['ext_service'],

'unit_services': r['unit_service'],

'int_services_unit': r['int_service_unit'],

'ext_services_unit': r['ext_service_unit'],

'service_type': r['service_type'],

'service_condition' : r['service_condition'],

}

ret.append(inputs)

print ('------------liste de services-------')

print ret

return ret


Try simpler case like this:

def func2(msg):
    return 'result of func2("' + func1(msg) + '")'

def func1(msg):
    return 'result of func1("' + msg + '")'

print func1('test')
print func2('test')


It prints:

result of func1("test")
result of func2("result of func1("test")")

Notice the order of function definitions is intentionally reversed. The order of function definitions does not matter in Python.

Regards.


0
jamesbale
On 5/3/13, 4:39 AM

to call a function from function it is simple, what are you doing a bit surprise me

def confirmation(self, cr, uid, ids, context={}):


def action_confirm_draft(self, cr, uid, ids, context={}):
    self.confirmation(cr, uid, ids, context)

what if i want to check the function self.confirmation(cr, uid, ids, context) is true or fales , den wat is the syntax

mihir shah
on 5/3/13, 4:51 AM

then your function shloud return True or False , and check like this if confirmation(cr, uid, ids, context): thats it...

jamesbale
on 5/3/13, 6:44 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 5/3/13, 4:23 AM
Seen: 5944 times
Last updated: 8/13/15, 5:19 AM