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.

0

Calling a python method directly from menuitem

By
Timo Talvitie
on 9/23/14, 5:03 AM 2,533 views

I would like to run some python code immediately when a user clicks a menu item. Old action-related documention mentions execute as a possible action type:

"Execute: The execution of a method on the server side"

...but there seems to be no other documentation or examples. Has anyone succeeded in doing something similar?

2
Vivek
On 9/30/15, 2:58 PM

Hi

There is one way to run python code with click on menu item. By using server action, you can achieve this. You need to make one server action and set it as action parameter in menu item.

Here is an example:

Server Action

<record id="action_make_testing" model="ir.actions.server">

<field name="name">Make Testing</field>

<field name="condition">True</field>

<field name="type">ir.actions.server</field>

<field name="model_id" ref="model_res_partner" />

<field name="state">code</field>

<field name="code">self.test_act(cr, uid, context.get('active_ids', []), context=context)</field>

</record>

 In this server action, there is one field "code". You need to set method which you want to call with click on menu. Another one is "model_id". You need to set external id of model in which you define (or make) method, which you want to call. Here, I set "model_res_partner" as model_id. It means, i have defined method "test_act" in res.partner model.

Menu:

<menuitem id='menu_testing' name='Testing' sequence="100" parent="base.menu_base_config"

     action="action_make_testing"/>

In menuitem, you need to set external id of server action into action parameter.

Python code to run : Method name = test_act

class res_partner(osv.osv):

     _inherit = 'res.partner'

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

         return True 

Took me a while to return to this issue, but this solution works like a charm. Thanks!

Vizucom Oy, Timo Talvitie
on 2/2/16, 6:44 AM

How can we do it in Odoo 9 using api?

dirtyHandsPHP
on 10/9/16, 4:27 PM
0
T. Major
On 9/30/15, 9:05 AM

Hi, have you already been successful calling a python method directly from a menuitem?

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: 9/23/14, 5:03 AM
Seen: 2533 times
Last updated: 2/2/16, 6:43 AM