在网站上创建按钮,在ODOO中运行python脚本

问题描述 投票:0回答:2

我在odoo中制作了一个新模块。它有动作按钮(使用ipwhois检查ip)。现在我需要将此模块集成到网站中。在网页上显示字段很容易,但我不知道如何制作按钮并调用该功能。该按钮显示在网页上,但功能不可调用。

据我所知:点击按钮必须调用python函数,重新加载页面并在字段“result_check”中写入一个新值。

<record id="sale_form_view" model="ir.ui.view">
  <field name="name">sale.form</field>
  <field name="model">product.template</field>
  <field name="inherit_id" ref="website_sale.product_template_form_view"/>
  <field name="arch" type="xml">
    <field name="categ_id" position="after">
      <button name="make_request" type="object" string="Make check" />
      <field name="result_check" />
    </field>
  </field>
</record>

<templateid="add_fields_product"inherit_id="website_sale.product">
<xpathexpr="//div[@id='product_details']"position="before"> 
<div>
   <a role="button" href="#"><span>Make check</span></a> 
   <p itemprop="name" t-field="product.result_check">Result</p> 
</div> 
</xpath>
</template>
python odoo odoo-12
2个回答
0
投票

首先,您必须在product_template.py file.Means中编写make_request函数,您必须继承product.template模型。如下所示:

    def make_request(self):
        # Here your code...

从此函数中,您可以设置“result_check”字段的值。

我希望这可以帮助你。谢谢。


0
投票

您需要将代码放在models.py文件中。你的xml文件有

<button name="make_request" type="object" string="Make check" />

当用户单击此按钮时,它将转到models.py文件以激活该按钮。看起来你使用类:product.template [来自你的xml文件]

在models.py文件中,找到product.template部分,在这个类中放入:

@api.multi
def make_request(self):
    Put your code here
    return

所以现在xml中的按钮链接到python代码。这就是你如何获得一个按钮来激活你的python代码(使你的按钮调用你的python代码)

© www.soinside.com 2019 - 2024. All rights reserved.