odoo form视图添加按钮跳转其他视图

要求:

 在采购订单中添加入库明细按钮,跳转到该订单的入库明细页面,如下所示:

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过滤条件,过滤当前的采购订单。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值