In Odoo, both XML and Python can be used to pass relevant data by
leveraging the concept of context. In Python, a context typically
refers to a dictionary that stores key information, helping
functions operate with specific parameters or environmental cues.
This becomes especially useful when writing modular and reusable
code. Most methods in Odoo accept a context parameter, enabling the
seamless flow of additional data. The examples below demonstrate
various practical ways to utilize context effectively across
different use cases.
1. To pass default values for fields:
<field name="work_location_id" context="{'default_address_id': address_id}" />
This approach allows you to set a default value for a specific field.
2. Setting default filters and groups by records:
<group expand="0" string="Group By">
<filter string="Department" name="department" domain="[]"
context="{'group_by': 'department_id'}"/>
<filter string="Company" name="company" domain="[]"
context="{'group_by': 'company_id'}" groups="base.group_multi_company"/>
<filter string="Employment Type" name="employment_type" domain="[]"
context="{'group_by': 'contract_type_id'}"/>
</group>
The context can be configured to include a group_by field to enable
grouped data views.
3. In Window actions
<record id="crm_lead_action_my_activities" model="ir.actions.act_window">
<field name="name">My Activities</field>
<field name="res_model">crm.lead</field>
<field name="view_mode">list,kanban,graph,pivot,calendar,form,activity
</field>
<field name="view_id" ref="crm_lead_view_list_activities"/>
<field name="domain">[('activity_ids','!=',False)]</field>
<field name="search_view_id"
ref="crm.view_crm_case_my_activities_filter"/>
<field name="context">{'default_type': 'opportunity',
'search_default_assigned_to_me': 1}
</field>
<field name="help" type="html">
<p class="o_view_nocontent_smiling_face">
Looks like nothing is planned.
</p>
<p>
Schedule activities to keep track of everything you have to do.
</p>
</field>
</record>
To assign default values to newly created records, you can use the
context within window actions. For instance, the default type can be
set to 'opportunity'.
4. Python as a function
Context is also supported within Python methods. You can extract
values passed from XML using the context dictionary. For example,
env.context.get('partner_id') retrieves the value associated with
partner_id, which may have been provided via an XML action, such as
self.partner_id.
5. In search view and filters
<group expand="0" string="Group By">
<filter string="User" name="user" domain="[]"
context="{'group_by':'user_id'}"/>
<filter string="Type" name="type" domain="[]"
context="{'group_by':'resource_type'}"/>
<filter string="Company" name="company" domain="[]"
context="{'group_by':'company_id'}"
groups="base.group_multi_company"/>
<filter string="Working Time" name="working_period" domain="[]"
context="{'group_by':'calendar_id'}"/>
</group>
Two filters are defined within the group tag, and the group_by
parameter is passed using the context.