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.
2. INTRODUCTION
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 user interface. Creating normal views from xml is explained in our previous blog
: https://www.cybrosys.com/blog/odoo-views
3. Here I would like to share some of the cool features you can use while working with
Odoo XML.
4. 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.
<span t-
field="request.env['model.name'].search([],limit=1).a_field
_name"/>
5. Accessing Session
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
request.session.uid
Current databses using
request.session.db
Usage : <t t-esc="request.session.uid"/>
<t t-esc="request.session.db"/>
6. 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.
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" />
7. Widgets
Widgets can be used to make Odoo interface more user-friendly. We can change the appearance of a fie
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
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.
8. t-esc and t-field
t-esc takes an expression, evaluate 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 case of a ‘field’ in a model it is better to use 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 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.
9. Refer this link for more:
https://www.cybrosys.com/blog/xml-operations-in-odoo
10. Thank You !
Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park,
Kakkancherry,
Calicut University P.O.
Calicut
Kerala, India - 673635.
Cybrosys Ltd
15, ST Antonys Road,
Forest Gate, London
England,
E79QA.
Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, Kerala,
India-682030.