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.
<span t-field="request.env['model.name'].search([],limit=1).a_field_name"/>
A python parsel file for an XML is only required when we need complex calculations for the reports.
Accessing Session
We can access the current session using ‘request.session’ from the XML file. Any value stored in the session object can be obtained from it. For example, current user id can be obtained by
request.session.uid
Current databses using
request.session.db
Usage:
<t t-esc="request.session.uid"/>
<t t-esc="request.session.db"/>
Accessing current user record
Any column in the 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.
Usage:
<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
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"/>
progress bar: 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” creates an 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, evaluates it, and prints the content.
<p><t t-esc="value"/></p>
It can be also used to get values from an object:
<p><t t-esc="object.value"/></p>
But in the case of a ‘field’ in a model, it is better to use a t-field.
<p><t t-esc="model.field_name"/></p>
Note: If you use t-esc to render field, we won't be able to print fields that 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.