如何从 XML 服务器操作调用 python 方法?
how to call python method from XML server action?
我在 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 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>
你的计算字段依赖于其他项目的名称,在odoo中你可以像这样依赖相关字段,你可以深入到你想要的,不仅是many2one,你也可以使用X2many
@api.depends('item.name','dsc.name', 'drc.name', 'org.name','car.name','model.name', 'manf.name','year.name')
这样,即使您在其他视图中更改其中一个名称,该字段也会在后台重新计算。我希望这可以帮助您无需为此任务创建按钮
我在 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 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>
你的计算字段依赖于其他项目的名称,在odoo中你可以像这样依赖相关字段,你可以深入到你想要的,不仅是many2one,你也可以使用X2many
@api.depends('item.name','dsc.name', 'drc.name', 'org.name','car.name','model.name', 'manf.name','year.name')
这样,即使您在其他视图中更改其中一个名称,该字段也会在后台重新计算。我希望这可以帮助您无需为此任务创建按钮