Development Book V17: Context

By utilizing XML or Python, it becomes possible to transmit information within a given context. Hence, when writing code, having context becomes increasingly beneficial. In Python, a context refers to a dictionary that aids in conveying essential information to an Odoo function. It is noteworthy that nearly every method in Odoo incorporates a context argument for data transmission. Various examples showcasing different ways to employ the context are provided below.

1. To pass default values for fields:

< field name="work_location_id" context="{'default_address_id': address_id}" />

By doing this, you can establish the default value for a 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>

Within the context, you have the option to include the group_by field.

3. In Window actions

< record id="crm_lead_action_my_activities" model="ir.actions.act_window">
   < field name="name">My Activities
   < field name="res_model">crm.lead
   < field name="view_mode">tree,kanban,graph,pivot,calendar,form,activity
   < /field>
   < field name="view_id" ref="crm_lead_view_list_activities"/>
   < field name="domain">[('activity_ids','!=',False)]
   < field name="search_view_id"
   < 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 define default values for new records, utilize the context in window actions. In this case, the default type is configured to be opportunity.

4. Python has a function

Context is applicable within Python functions as well. You can retrieve any value from XML passed to your Python function, as demonstrated in this statement: env.context.get('partner id'), where self.partner_id is the value passed from the XML.

5. In search view and filters

< group expand="0" string="Group By">
   < filter string="User" name="user" domain="[]"
   < filter string="Type" name="type" domain="[]"
   < filter string="Company" name="company" domain="[]"
   < filter string="Working Time" name="working_period" domain="[]"
< /group>

Two filters have been included within the group tag, and the group_by has been transmitted by leveraging the context.



Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635



Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.



Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message