要求:
在采购订单中添加入库明细按钮,跳转到该订单的入库明细页面,如下所示:
xml代码:
<field name="inherit_id" ref="purchase.purchase_order_form"/>
<field name="arch" type="xml">
<xpath expr="//div[@name='button_box']" position="inside">
<button type="object" name="action_view_stock_detail"
class="oe_stat_button"
icon="fa-pencil-square-o" string="入库明细">
</button>
</xpath>
</field>
使用视图继承的方式,然后button标签添加按钮,expr属性即用xpath语法定位,position属性设置插入位置,icon设置按钮图标,string设置按钮显示文字,也可以在button标签中嵌套field标签,则按钮的文字即显示field的文本,按钮的name属性为python后端所调用的函数名,点击按钮则调用后端函数。
还有这样的场景:比如说在采购订单上显示这个按钮,而在询价单上不显示,我们可以通过设置按钮的属性来控制可见性,代码如下:
<xpath expr="//div[@name='button_box']" position="inside">
<button type="object" name="action_view_stock_detail"
class="oe_stat_button"
icon="fa-pencil-square-o" string="入库明细"
attrs="{'invisible':[('state','not in',('done','purchase'))]}">
</button>
</xpath>
python代码如下所示:
class PurchaseOrder(models.Model):
_inherit = "purchase.order"
# 跳转入库明细
def action_view_stock_detail(self):
domain = []
domain.append(('purchase_order_id', '=', self.id))
domain.append(('purchase_order_id', '!=', False))
return {
'name': (u'采购入库明细'),
'type': 'ir.actions.act_window',
'res_model': 'stock.move.line',
'view_mode': 'tree,form',
'views': [(self.env.ref('purchase_information_report.purchase_order_inventory_detail_tree').id, 'tree')],
'target': 'current',
'domain': domain
}
该函数返回一个已经定义好了的动作视图,只不过加上了domain过滤条件,过滤当前的采购订单。