python - Odoo 10 : Cant call my python function from menu item

标签 python xml odoo

这是我的 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>

这是我在 models.py 中的 python 函数:

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

当我点击我的菜单项时,我无法调用这个函数。我得到一个错误:

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

在odoo 10中调用函数的方式是否正确?我如何调用我的函数或在 views.xml 中定义 self ?

最佳答案

您可以在 Odoo 的技术部分创建 ir.actions.server 或简单的服务器操作。它对初学者有一些有趣的优势:一点文档。以下是 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 = {...}

并且您来自菜单,因此 Odoo 不知道任何记录。只需使用 model.on_test() 作为 PROTOCOL 已经编写的。您还应该用 @api.model 修饰您的方法,以告诉 Odoo 在该方法的调用中不涉及任何记录。

关于python - Odoo 10 : Cant call my python function from menu item,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52294170/

相关文章:

python - MNIST Python numpy 特征向量可视化错误

python - 类型错误 : unsupported operand type(s) for +: 'bool' and 'str' - Odoo v8 to Odoo v10 migration

python - SessionNotCreatedException : Message: session not created from tab crashed in Python with Odoo, 但是 selenium 单独工作很好,

crash - 奥多 11 : Cant update module because new template was added to the module

python - 数据框对象没有属性

python - 强制使用 virtualenv pip

python - 直接从html模板中删除用户

python - 在 Python 中解析 XML 节点中的文本

java - Log4j 日志附加到取决于日期的文件名 - 配置

java - 如何在 Spring 中使用 setter 注入(inject)和 java 配置?