Odoo views are dynamically created from XML codes. Data of Odoo are stored as objects and XML description is used to expose these objects to the user interface.
Creating normal views from XML is explained in our previous blog: Odoo views
Here I would like to share some of the cool features you can use while working with Odoo XML.
Searching for a model
Many people use a separate parser for XML reports just to get a value from another model. But we can call any model from XML itself using flowing code.
A python parsel file for an XML is only required when we need complex calculation for the reports.
We can access the current session using ‘request.session’ from the XML file. Any value stored in session object can be obtained from it. For example, current user id can be obtained by
Current databses using
Accessing current user record
Any column in user table can be obtained by just using ‘user.column(field_name)’ from XML files. For example, ‘user.name’ will give you the name of the user, ‘user.groups_id’ will give all the current users in the group.
<field name="domain_force" > ['|', ('public', '=', 'groups'), ('group_public_id', 'in', [g.id for g in user.groups_id])] </field>
<strong t-field="user.name" />
Widgets can be used to make Odoo interface more user-friendly. We can change the appearance of a field without making changes in database columns.
<field name="field_name" widget="widget_name"/>
progressbar : widget=”progressbar” will create a progress bar for float or Integer fields.
float_time : widget=”float_time” is used to convert float field into time format, That is, 1.00 will be converted to 01:00.
url : widget=”url” create http link for the field.
email: widget=”email” will create a link to send the email.
Monetary: widget=”monetary” can be used with float field to show currency sign after the field.
t-esc and t-field
t-esc takes an expression, evaluate it and prints the content.
It can be also used to get values from an object:
But in case of a ‘field’ in a model it is better to use t-field.
Note: If you use t-esc to render field, we won't be able to print fields which are not stored (like related fields, compute field without store = True etc.)
Note: t-field can only be used when performing field access (a.b). It is able to automatically format based on the field type.