如何从XML服务器操作调用python方法?

2024-05-17 01:04:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我在product.template中添加了一个方法来计算与其他模型相关的一些自定义字段的产品名称表单字符串, 我在名称字段中添加了store=True,这样我就可以对其进行搜索,但当相关字段在其模型上更改时,字段名称不会更改,因此我需要将服务器操作添加到名为“update product name”的菜单项中,该菜单项位于cutome应用程序上,相关字段位于该菜单项中,如果我按下这个菜单项,它将运行命名方法,并在产品名称上获得新的更新字段 我该怎么做 这是密码

class autopart(models.Model):
_inherit = 'product.template'

@api.multi
@api.depends('item','dsc', 'drc', 'org','car','model', 'manf','year')
def naming(self):
    for rec in self:
        if rec.year:
            rec.name = " ".join(
                [rec.item and rec.item.name or "", rec.drc and rec.drc.name or "", rec.dsc and rec.dsc.name or "",
                 rec.org and rec.org.name or "", rec.manf and rec.manf.name or "", rec.car and rec.car.name or "",
                 rec.model and rec.model.name or "",rec.year and rec.year.name or ""
                 ])
        else:
            rec.name = " ".join(
                [rec.item and rec.item.name or "", rec.drc and rec.drc.name or "", rec.dsc and rec.dsc.name or "",
                 rec.org and rec.org.name or "", rec.manf and rec.manf.name or "", rec.car and rec.car.name or "",
                 rec.model and rec.model.name or "",
                 ])
 name = fields.Char(string="Name", compute=naming ,store=True , required=False,)

这是menuitem

 <menuitem id="update_products_menu" name="Update products" parent="master.menu_category_main" sequence="1" action="action_update_products"/>

服务器操作

    <record id="action_update_products" model="ir.actions.server">
        <field name="name">action_update_products</field>
        <field name="type">ir.actions.server</field>
        <field name="model_id" ref="model_updateproducts"/>
        <field name="state">code</field>
        <field name="code">how can i run naming methode from product.template here</field>
    </record>

Tags: orandnameorgfieldmodelupdateproduct
1条回答
网友
1楼 · 发布于 2024-05-17 01:04:56

您的计算字段取决于其他项的名称,在odoo中,您可以依赖类似于这样的相关字段,您可以根据需要深入到任意深度,而不仅仅是一个字段,您还可以使用多个字段

@api.depends('item.name','dsc.name', 'drc.name', 'org.name','car.name','model.name', 'manf.name','year.name')

这样,即使在其他视图中更改其中一个名称,也会在后台重新计算字段。我希望这有助于您无需为此任务创建按钮

相关问题 更多 >