Odoo 14列表视图:如何设置列始终可见并取消显示/隐藏选项
optional in Odoo 14 XML Templates Absolutely, you can make that code field stay permanently visible—here's exactly how to do it in your inherited XML template:
First, let's clarify how Odoo's optional attribute works: it only recognizes two valid values:
show: The field is displayed by default, but users can hide it via the column selector menu.hide: The field is hidden by default, but users can show it via the menu.
If the optional attribute is removed entirely or set to any value that isn't show/hide, the field becomes permanently visible—it won't even appear in the column selector menu at all. That's exactly what you want!
Option 1: Remove the optional Attribute (Recommended)
The cleanest approach is to strip away the optional attribute inherited from the parent template. Use this XML code:
<xpath expr="//field[@name='code']" position="attributes"> <attribute name="optional" remove="1"/> </xpath>
This directly removes the optional="show" setting, locking the field into always being visible.
Option 2: Set a Non-Valid optional Value
If you prefer not to remove the attribute, you can set it to a value Odoo doesn't recognize (like "always"). This will also prevent the field from being toggleable:
<xpath expr="//field[@name='code']" position="attributes"> <attribute name="optional">always</attribute> </xpath>
While this works, removing the attribute is more aligned with Odoo's intended behavior.
Quick Notes to Avoid Issues:
- Double-check your XPath expression to make sure it targets the correct
codefield. If there are multiple views with this field, narrow the scope (e.g., use//tree//field[@name='code']to target only tree views). - Ensure your module loads after the module that defines the parent template (adjust dependencies in your
__manifest__.pyif needed).
内容的提问来源于stack exchange,提问作者Lukas




