콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
5 답글
33171 화면

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?

아바타
취소
베스트 답변

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!

How can we do it in Odoo 9 using api?

베스트 답변

Here is my solution for Odoo 11:

*.xml

<record model="ir.actions.server" id="action_upd_printed_invoices">
<field name="name">Invoice Upd 'printed' Server Action</field>
<field name="model_id" ref="model_invoice_printed_upd"/>
<field name="type">ir.actions.server</field>
<field name="state">code</field>
<field name="code">model.on_test_server_action()</field>

</record>

<menuitem id="menu_upd_printed_invoices" name="Some Text..." action="action_upd_printed_invoices" parent="account.menu_finance_configuration" sequence="500"/>

invoice_printed_upd.py

def on_test_server_action(self):
raise Warning(_('Test action!'))


Cheers!

아바타
취소
베스트 답변

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

아바타
취소
관련 게시물 답글 화면 활동
1
7월 22
1892
2
8월 23
2406
0
1월 23
1987
2
12월 19
10410
0
11월 18
2698