In Odoo 17, a transformation has been implemented to enhance the definition of view modifiers—such as required, readonly, and invisible—within XML views.
Previously, these modifiers were managed through the attrs attribute and states attribute. The new approach simplifies and refines this process, making it more intuitive and efficient for developers. In this article we will explore these changes and how they improve the overall user experience.
Operation Before the Change
Before Odoo 17, defining view modifiers involved multiple, sometimes conflicting methods. The complexity of combining various attributes such as attrs, states, and their Python expressions made it difficult to create and manage views efficiently. Here are the key issues:
-
Multiple Methods for Modifiers: The attributes
required, readonly,
and invisible could be defined directly
as True, False,
or using a Python expression. For example:
<page invisible="not context.get('show_me')"/> used a Python expression to set the visibility. -
Attrs Attribute: The attrs
attribute used a dictionary to define multiple conditions for the
required, readonly,
and invisible states. For example
<field name="total" attrs="{'invisible': [('name', '=', 'red')], 'required': [('tag_id', 'in', uid)]}"/>. - Sates Attribute: The states attribute specified visibility or accessibility of fields based on a comma-separated list of states. For instance, <page states="draft,done"/> would make the page visible in the draft and done states.
- Python Field States: In the Python models, the states
parameter could define conditions for fields' visibility or access.
For example:
fields.Boolean(readonly=True, states={'draft': [('readonly', False)], 'done': [('readonly', False)]})
This combination of methods caused confusion, especially when these different approaches had to be evaluated and processed through JavaScript, leading to post-processing in Python and evaluation in JavaScript.
The invisible attribute, in particular, was split into two uses: invisible and column_invisible.
The Goal of the Change
The primary objective of the Odoo 17 update is to simplify the way view modifiers are defined by allowing Python expressions to directly control visibility, access, and state-related attributes. These expressions are now evaluated in JavaScript without requiring any post-processing in Python, making the system more efficient and consistent.
Example of Modifiers in Odoo 17
Here’s an example of how modifiers were defined before and after the change:
Before the change:
<field name="field_a" readonly="not context.get('show_a')" attrs="{'readonly': [('field_b', '!=', False), ('field_c', '=', parent.c)]}"/> <field name="field_b" states="draft"/>
After the change:
<field name="field_a" readonly="not context.get('show_a') or field_b and field_c == parent.c"/> <field name="field_b" invisible="state != 'draft'"/>
The new approach reduces redundancy by merging conditions directly into the readonly or invisible attributes. Instead of handling states separately, it is now integrated as part of the visibility condition using Python logic.
Example of Updating Inherited Views in Odoo 17
Inherited views are also affected by the changes in Odoo 17. Here is how an inherited view that uses the attrs attribute would be modified:
Before the change:
<field name="field_a" readonly="not context.get('show_a')" attrs="{'invisible': [('field_b', '!=', False)]}"/> <field name="field_a" position="attributes"> <attribute name="attrs">{'readonly': [('field_c', '=', False)], 'invisible': [('field_d', '!=', '3')]}</attribute> </field>
After the change:
<field name="field_a" readonly="not context.get('show_a')" invisible="field_b"/> <field name="field_a" position="attributes"> <attribute name="readonly" add="(not field_c)" separator=" or "/> <attribute name="invisible">field_d != 3</attribute> </field>
In this case, the complex attrs attribute has been replaced by straightforward readonly and invisible conditions, improving readability and reducing the potential for errors.
Also there is an OCA module that can help with transition to Odoo v17
Conclusion
The Odoo 17 update to modifiers brings significant improvements in terms of simplicity and consistency. By replacing the attrs and states attributes with Python expressions evaluated in JavaScript, Odoo has streamlined the process of defining view modifiers. This change not only improves the user experience but also makes the system more efficient by eliminating the need for post-processing. Developers can now work with a more intuitive and powerful system for managing view states and field visibility.