如何在Odoo 8的树视图中使用XML attrs属性取决于Many2one?

时间:2021-08-27 23:51:09

I am working with stock.move model. This model has a Many2one field named picking_type_id. I know this is not the best way, but I use attrs="{'invisible': [('picking_type_id', '=', 1)]}" expression to hide elements in incoming stock moves (I am pretty sure that incoming type ID is not going to be modified).

我正在使用stock.move模型。此模型具有名为picking_type_id的Many2one字段。我知道这不是最好的方法,但我使用attrs =“{'invisible':[('picking_type_id','=',1)]}”表达式来隐藏传入股票行动中的元素(我很确定传入类型ID不会被修改)。

But this expression is only working in form views, it does not work for tree views. This would be comprensible if the element is a field, but if not (for example a button), it should work, shouldn't it?

但是这个表达式只在表单视图中工作,它不适用于树视图。如果元素是一个字段,这将是不可能的,但如果不是(例如一个按钮),它应该工作,不应该吗?

<field name="picking_type_id" invisible="0"/>
<button name="open_lots_manager_wizard"
    string="Select lots" type="object"
    icon="terp-accessories-archiver+"
    attrs="{'invisible': [('picking_type_id', '=', 1)]}"
    groups="stock.group_stock_user"/>

For example if I modify the above attrs expression and turn it into the below one, it works (if I set a quantity over 3, the button disappears, and it appears again if I set a quantity under or equal to 3):

例如,如果我修改上面的attrs表达式并将其转换为下面的表达式,它就可以工作(如果我设置的数量超过3,则按钮会消失,如果我将数量设置为小于或等于3,它会再次出现):

<field name="picking_type_id" invisible="0"/>
<button name="open_lots_manager_wizard"
    string="Select lots" type="object"
    icon="terp-accessories-archiver+"
    attrs="{'invisible': [('product_uom_qty', '>', 3)]}"
    groups="stock.group_stock_user"/>

Can anyone explain me the reason of this? Do I have to create a related field pointing to the picking type code (for example) only to achieve my purpose?

谁能解释一下这个原因?我是否必须创建指向拾取类型代码的相关字段(例如)才能实现我的目的?

1 个解决方案

#1


1  

I tested your code and understand problem. It is better to get the object per XML ID (stock.picking_type_in) and compare it with the picking type in stock move.

我测试了你的代码并理解了问题。最好每个XML ID(stock.picking_type_in)获取对象,并将其与库存移动中的拾取类型进行比较。

My solution looks like this.

我的解决方案看起来像这样

test_stock_move.py

test_stock_move.py

# -*- coding: utf-8 -*-
from openerp import models, fields, api, _

class stock_move(models.Model):
    _inherit = "stock.move"

    @api.multi
    @api.depends("picking_type_id")
    def _compute_incoming_type(self):
        for o in self:
            o.is_picking_type_incoming = (o.picking_type_id.id == self.env.ref("stock.picking_type_in").id)

    is_picking_type_incoming = fields.Boolean(_("Is picking type incoming"),compute=_compute_incoming_type)

stock_move_view.xml

stock_move_view.xml

<record id="stock_move_tree" model="ir.ui.view">
    <field name="name" >stock.move.form</field>
    <field name="model">stock.move</field>
    <field name="inherit_id" ref="stock.view_move_tree" />
    <field name="arch" type="xml">
        <xpath expr="//field[@name='state']" position="after">
              <field name="is_picking_type_incoming"/>
              <button name="open_lots_manager_wizard"
                  string="Select lots" type="object"
                  icon="terp-accessories-archiver+"
                  attrs="{'invisible': [('is_picking_type_incoming', '=', True)]}"
                  groups="stock.group_stock_user"/>
         </xpath>
    </field>
</record>

I added a new computed field is_picking_type_incoming in stock.move model. That worked in my case. Hopefully it solves your problem.

我在stock.move模型中添加了一个新的计算字段is_picking_type_incoming。这在我的情况下有效。希望它能解决你的问题。

#1


1  

I tested your code and understand problem. It is better to get the object per XML ID (stock.picking_type_in) and compare it with the picking type in stock move.

我测试了你的代码并理解了问题。最好每个XML ID(stock.picking_type_in)获取对象,并将其与库存移动中的拾取类型进行比较。

My solution looks like this.

我的解决方案看起来像这样

test_stock_move.py

test_stock_move.py

# -*- coding: utf-8 -*-
from openerp import models, fields, api, _

class stock_move(models.Model):
    _inherit = "stock.move"

    @api.multi
    @api.depends("picking_type_id")
    def _compute_incoming_type(self):
        for o in self:
            o.is_picking_type_incoming = (o.picking_type_id.id == self.env.ref("stock.picking_type_in").id)

    is_picking_type_incoming = fields.Boolean(_("Is picking type incoming"),compute=_compute_incoming_type)

stock_move_view.xml

stock_move_view.xml

<record id="stock_move_tree" model="ir.ui.view">
    <field name="name" >stock.move.form</field>
    <field name="model">stock.move</field>
    <field name="inherit_id" ref="stock.view_move_tree" />
    <field name="arch" type="xml">
        <xpath expr="//field[@name='state']" position="after">
              <field name="is_picking_type_incoming"/>
              <button name="open_lots_manager_wizard"
                  string="Select lots" type="object"
                  icon="terp-accessories-archiver+"
                  attrs="{'invisible': [('is_picking_type_incoming', '=', True)]}"
                  groups="stock.group_stock_user"/>
         </xpath>
    </field>
</record>

I added a new computed field is_picking_type_incoming in stock.move model. That worked in my case. Hopefully it solves your problem.

我在stock.move模型中添加了一个新的计算字段is_picking_type_incoming。这在我的情况下有效。希望它能解决你的问题。