简体   繁体   English

Odoo 10:无法从菜单项调用我的python函数

[英]Odoo 10: Cant call my python function from menu item

Here it is my view.xml : 这是我的view.xml:

<?xml version="1.0" encoding="utf-8"?>
<odoo>
  <data>
<record id="view_sim" model="ir.actions.server">

            <field name="name">Details</field>
             <field name="model_id" ref="model_test" />   
            <field name="condition">True</field>

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

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

            <field name="code">self.on_test()</field>

    </record>

    <record model="ir.actions.act_window" id="view_sim">
        <field name="name">Details</field>
        <field 
         name="res_model">test</field>
        <field name="view_type">form</field>
        <field name="limit">100</field>
        <field name="view_mode">tree,form</field>
        <field name="domain">[]</field>
        <field name="help" type="html">
            <p class="oe_view_nocontent_create">Create new
            </p>
        </field>
</record>


    <!-- Top menu item -->
     <menuitem id="root.menu_root" name="card"/>
     <!-- menu categories -->
        <menuitem id="sim" name="Sim" parent="root.menu_root" action="view_sim"/>

  </data>
</odoo>

Here is my python function in models.py : 这是我在models.py中的python函数:

def on_test(self):
          _logger.error("test")

I am not able to call this function when i click on my menuitem.I get an error : 当我点击我的menuitem时,我无法调用此函数。我收到错误:

ValueError: <type 'exceptions.NameError'>: "name 'self' is not defined" while evaluating
u'self.on_test()

Is it the right way to call the function in odoo 10? 这是在odoo 10中调用函数的正确方法吗? How can i call my function or define self in views.xml? 如何在views.xml中调用我的函数或定义self?

Try this, Instead of 试试这个,而不是

<field name="code">self.on_test()</field>

change to 改成

<field name="code">model.on_test()</field>

You can create ir.actions.server or simple Server Actions in the technical part of Odoo. 您可以在ir.actions.server的技术部分中创建ir.actions.server或简单的Server Actions。 It has some interesting advantage for beginners: a little documentation. 它为初学者提供了一些有趣的优势:一点文档。 Following is a copy from Odoo 10 and shows the little documentation you get on creating a python code server action: 以下是Odoo 10的副本,并显示了创建python代码服务器操作时获得的小文档:

# Available variables:
#  - time, datetime, dateutil, timezone: Python libraries
#  - env: Odoo Environement
#  - model: Model of the record on which the action is triggered
#  - record: Record on which the action is triggered if there is one, otherwise None
#  - records: Records on which the action is triggered if there is one, otherwise None
#  - log : log(message), function to log debug information in logging table
#  - Warning: Warning Exception to use with raise
# To return an action, assign: action = {...}

And you're coming from a menu, so Odoo does not know about any records. 你来自菜单,所以Odoo不知道任何记录。 Just use model.on_test() as PROTOCOL already has written. 只需使用model.on_test()作为PROTOCOL已编写。 You should also decorate you method with @api.model to tell Odoo that there aren't any records involved in the call of this method. 您还应该使用@api.model修饰您的方法,告诉Odoo此方法的调用中没有涉及任何记录。

Useful link where you could find the documentation of the available options in the evaluation context of Odoo Server Actions 有用的链接,您可以在Odoo Server Actions的评估上下文中找到可用选项的文档

http://www.odoo.com/documentation/10.0/reference/actions.html#reference-actions-server-context http://www.odoo.com/documentation/10.0/reference/actions.html#reference-actions-server-context

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM