Odoo树视图按条件隐藏计算字段:库存历史报表视图需求
Got it, let's break down how to conditionally hide computed fields like inventory_value in your view_stock_history_report_tree tree view. Odoo gives you a few flexible ways to handle this, depending on your specific condition requirements:
1. Use attrs for Record-Based Conditions
If you want to hide the field based on values in the current record (like source, location_id, or product attributes), use the attrs attribute to define a visibility domain.
For example, to hide inventory_value when the source is marked as "internal":
<tree string="Stock Value At Date" create="0" delete="0"> <!-- ... other existing fields ... --> <field name="inventory_value" sum="Total Value" attrs="{'invisible': [('source', '=', 'internal')]}"/> </tree>
2. Control Visibility via Context
If you need to toggle visibility based on how the view is opened (e.g., from a specific menu or action), use Odoo's context variable directly in the invisible attribute.
First, add a context flag when triggering the view (e.g., in an action definition):
<record id="action_stock_history_custom" model="ir.actions.act_window"> <field name="name">Stock History (Hide Value)</field> <field name="res_model">stock.history</field> <field name="view_mode">tree</field> <field name="context">{'hide_inventory_value': True}</field> </record>
Then update your tree view to check for this context flag:
<tree string="Stock Value At Date" create="0" delete="0"> <!-- ... other existing fields ... --> <field name="inventory_value" sum="Total Value" invisible="context.get('hide_inventory_value', False)"/> </tree>
3. Restrict to Specific User Groups
If visibility should be limited to certain user roles, use the groups attribute to tie the field to specific security groups.
For example, only let stock managers see the inventory_value field:
<tree string="Stock Value At Date" create="0" delete="0"> <!-- ... other existing fields ... --> <field name="inventory_value" sum="Total Value" groups="stock.group_stock_manager"/> </tree>
4. Handle Complex Logic with a Computed Field
For more advanced conditions (e.g., combining user permissions, record data, or external factors), add a computed boolean field to your model and use it to control visibility.
First, extend the stock.history model (adjust the model name if yours differs):
from odoo import models, fields class StockHistory(models.Model): _inherit = 'stock.history' hide_inventory_value = fields.Boolean(compute='_compute_hide_inventory_value') def _compute_hide_inventory_value(self): for rec in self: # Add your custom logic here # Example: Hide if the location is a supplier location OR user is not a manager is_supplier_loc = rec.location_id.usage == 'supplier' is_not_manager = not self.env.user.has_group('stock.group_stock_manager') rec.hide_inventory_value = is_supplier_loc or is_not_manager
Then update your tree view to reference this computed field:
<tree string="Stock Value At Date" create="0" delete="0"> <!-- ... other existing fields ... --> <field name="hide_inventory_value" invisible="1"/> <field name="inventory_value" sum="Total Value" attrs="{'invisible': [('hide_inventory_value', '=', True)]}"/> </tree>
Key Notes
- Always prefer view inheritance over modifying the original XML directly (use
<xpath>to target fields in an inherited view) to avoid losing changes during module upgrades. - Test your conditions thoroughly to ensure they behave as expected across different scenarios (e.g., different user roles, record states).
内容的提问来源于stack exchange,提问作者fueggit

