Odoo 隐藏计算字段取决于树视图上的条件

Odoo hide calculated field depend on condition on tree view

我有 Odoo 库存插件的 view_stock_history_report_tree 视图:

<tree string="Stock Value At Date" create="0" delete="0">
                <field name="location_id" invisible="1"/>
                <field name="product_id" invisible="1"/>
                <field name="move_id"/>
                <field name="company_id" groups="base.group_multi_company"/>
                <field name="date"/>
                <field name="source"/>
                <field name="quantity" sum="# of Products "/>
                <field name="inventory_value" sum="Total Value"/>
            </tree>

如果总和不等于 0,我想隐藏计算数量字段。我尝试通过以下方式向数量字段添加属性:

<field name="quantity" sum="# of Products " attrs="{'invisible': [('quantity', '=', 0)]}"/>

但是没有任何反应。似乎 attrs 没有在这个领域工作。我不明白如何使用 attrs 访问计算量字段。

示例:

我想隐藏 Persona V 分组字段和 Horizon 零之黎明 分组字段。

问题是您告诉 Odoo:

如果列数量的值等于0,则将其值设为invisible

所以您隐藏了该列的所有零。

但首先,看看这个,以防万一:

You can't make invisible a column of a tree view, the behaviour of attrs is different from the one of the form view. I mean, when you add attrs to make a field invisible depending on a condition, the behaviours in both views are different:

Form view

If the condition is fulfilled and the field must be invisible, it just disappears, the label and the value are invisible.

Tree view

If the condition is fulfilled and the field must be invisible, only the value is invisible, but not the label (in this case, the column).

因此您将始终看到 quantity 列,只是某些行的值为空。

编辑

所以我猜你想隐藏 Persona VHorizo​​n Zero Dawn 组因为他们的记录数量之和为零,不是吗?

然后继承树视图的模型,修改其read_group方法。这对你有用:

@api.model
def read_group(self, domain, fields, groupby, offset=0, limit=None,
               orderby=False, lazy=True):
    # If user has grouped the tree view by location_id and product_id...
    if u'location_id' in groupby and u'product_id' in groupby:
        ids_to_show = []
        # Put here your code to fill in ids_to_show with the IDs of the records you want to show
        ...
        # Then, make the domain more restrictive:
        domain[0].append([u'id', u'in', [ids_to_show]])
        # The domain will automatically remove the groups with no
        # representation. For example, if the result of ids_to_show is [2, 7, 35],
        # and those three records have as product Last of Us (x2) and Call of
        # Duty, the agrupations of Persona V and Horizon Zero Dawn will be
        # invisible as they don't have records inside.
    return super(YourModel, self).read_group(
        domain, fields, groupby, offset=offset, limit=limit,
        orderby=orderby, lazy=lazy)