Working With Context And Domain In Odoo V9 - Surekha Technologies

Skip to Main Content

Working With Context And Domain In Odoo V9 - Surekha Technologies

Odoo Working With Context And Domain In Odoo V9 calendar June 29, 2017
  • icon
  • icon
  • icon
blog-banner

Context is a dictionary carries some information like user ID, user language, Timezone, etc by default. You can pass any kind of data/information in context as per your need either from xml or from python (methods) files and then based on that extra information you can write your process code.

Here, I am going to explain how to use context for relational fields and context to pass when fetching possible values with examples.

1. Use the context to pass in default values for relational object fields.

  • ?Set value in many2one field’s child field as default by using keyword defualt_<many2one field’s child field name>.
    • For example, set company in journal_id’s many2one field company_id.

<field name="journal_id" context="{'default_company_id': company_id}"

  • Use a context value in python function by fetching value from xml.
    • For example, suppose you need to access sale order field value directly into sale order line method through context. For that you can pass the sale order field value in order lines context through xml and access it in a python function as per below code.

Pass value in context

<page string="Order Lines">

<field name="'partner_id'" invisible="1"/> <!-- sale order field-->

<field name="line_ids" context="{'partner_id': partner_id}">

<form string="Sales Order Lines">

<!-- order lines fields -->

</form>

</field>

</field>

</page>

Access value in python function

def your_order_line_function(self):

# to fetch sale order partner from context.

partner = self.env.context.get('partner_id')

# rest of the code

return True

2. Use the context in _defaults method to set any field value.

_defaults = {

'order_partner_id' : context.get('partner_id', False), #return id or False

}

3.Use the context to pass default values for normal fields.

  • For example, Set default value in partner name.

<field name="partner_id" context="{'default_name': ‘Agrolait’}">

4. Use for Context is on buttons to pass parameters to object functions.

<button string="Submit" type="object" name="my_func" context="{'param' : 'param_val'}" />

5. Use for Context to set default filter and group by records.

  • For example, Set default filter to display only leave allocation request and group by all records with employees.

<record id="holidays_allocation_approve" model="ir.actions.act_window">

<field name="name">Leaves Allocation</field>

<field name="res_model">hr.holidays</field>

<field name="context">{'default_type':'add', 'group_by':'employee_id'}</field>

</record>

Domain is a condition which is used to filter your data or for searching. A domain expression is a list of conditions. Each condition is a ('field_name', 'operator', ‘value') tuple.

Where,

  • field_name : The field name in which you want to set domain.

  • value : The value is evaluated as a Python expression. It can use literal values, such as numbers, booleans, strings, or lists, and can use fields and identifiers available in the evaluation context.

  • operator :

    • The usual comparison operators are < , > , <= , >= , = , != .

    • '=like' matches against a pattern, where the underscore symbol, _ , matches any single character, and the percentage symbol, % , matches any sequence of characters.

    • 'like' matches against a '%value%' pattern. The 'ilike' is similar but case insensitive.

    • The 'not like' and 'not ilike' operators are also available.

    • 'child of' finds the children values in a hierarchical relation, for the models configured to support them.

    • 'in' and 'not in' are used to check for inclusion in a given list, so the value should be a list of values. When it is to be used When it is to be used in "to-many" relation field the ‘in’ operator behaves like a CONTAINS operator.

Here, I am going to explain how to use domain for relational fields and filters to apply when displaying existing records for selection with examples.

1. Use the domain to filter relational object fields records with condition.

  • Suppose you need to display only customers in partner field, so you can set it in domain as per below code.

n xml file

<field name="partner_id" domain="[('customer','=',True)]" />

or

In python file

partner_id = fields.Many2one('sale.order’, string='Customer',domain=[('customer', '=', true)])

  • Suppose you need to display projects for selected client/partner only, if partner is not selected then display all projects. In that case you can use the ternary operator in domain condition as shown in below code.

<field name="project_ids" widget="many2many_tags" domain="[('partner_id','=?',partner_id)]"/>

  • Use parent object field value in relational field using domain.

<field name="tax_id" widget="many2many_tags" domain="{[('company_id','=',parent.company_id)]}"/>

  • Use dynamic domain through onchange function by following code.

@api.onchange('project_ids')

def _onchange_project_ids(self):

domain = {}

partner_list = []

if not self.project_ids:

partner_obj = self.env['res.partner'].search([('customer', '=', True)])

for partner_ids in partner_obj:

partner_list.append(partner_ids.id)

# to assign parter_list value in domain

domain = {'partner_id': [('id', '=', partner_list)]}

return {'domain': domain}

2. Use the domain display specific records in listview based on condition.

  • For example to display only those orders which are in ‘sale’ state in sale order list view.

<record id="action_orders" model="ir.actions.act_window">

<field name="name">Sales Orders</field>

<field name="type">ir.actions.act_window</field>

<field name="res_model">sale.order</field>

<field name="view_type">form</field>

<field name="view_mode">tree,kanban,form,calendar,pivot,graph</field>

<field name="search_view_id" ref="view_sales_order_filter"/>

<field name="domain">[('state', 'in', (‘sale’))]</field>

</record>

3. Use the domain in search view filter.

<search string="Product">

<!-- searches on multiple fields at once -->

<field name="name" string="Sales Order" filter_domain="['|',('product_variant_ids.name','ilike',self),('product_variant_ids.attribute_value_ids.name','ilike',self)]"/>

<filter string="Services" name="services" domain="[('type','=','service')]"/>

</search>

4. Use the domain in fields_view_get() method to set dynamic values for domain filters for a particular field.

  • For example set department id of the login users.

def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):

login_user_dpt_id = users_obj.browse(cr, SUPERUSER_ID, uid, context=context).emp_department_id

for node in eview.xpath("//field[@name='user_id']"):

if login_user_dpt_id:

user_filter = "[('emp_department_id', '='," + str(login_user_dpt_id.id) + " )]"

node.set('domain',user_filter)

5. Use the domain in record rule.

  • For example, set domain force to display own payslip which is in draft and done state to that login user.

<record model="ir.rule" id="property_rule_employee_payslip">

<field name="name">Employee Payslip</field>

<field name="model_id" ref="model_hr_payslip"/>

<field name="domain_force">[('employee_id.user_id', '=', user.id),('state','in',['done','draft'])]</field>

<field name="perm_create" eval="False"/>

<field name="perm_write" eval="False"/>

<field name="perm_unlink" eval="False"/>

<field name="groups" eval="[(4,ref('base.group_hr_user')),(4,ref('base.group_user'))]"/>

</record>

Basically domain is a filter you cannot expand beyond - context is a filter you can expand to the point that domain allows

Contact us

For Your Business Requirements

Name* Phone Number* Flag +1
  • +93 - AFG
  • +358 - ALA
  • +355 - ALB
  • +213 - DZA
  • +1 - ASM
  • +376 - AND
  • +244 - AGO
  • +1 - AIA
  • +672 - ATA
  • +1 - ATG
  • +54 - ARG
  • +374 - ARM
  • +297 - ABW
  • +61 - AUS
  • +43 - AUT
  • +994 - AZE
  • +1 - BHS
  • +973 - BHR
  • +880 - BGD
  • +1 - BRB
  • +375 - BLR
  • +32 - BEL
  • +501 - BLZ
  • +229 - BEN
  • +1 - BMU
  • +975 - BTN
  • +591 - BOL
  • +599 - BES
  • +387 - BIH
  • +267 - BWA
  • +47 - BVT
  • +55 - BRA
  • +246 - IOT
  • +1 - VGB
  • +673 - BRN
  • +359 - BGR
  • +226 - BFA
  • +95 - MMR
  • +257 - BDI
  • +855 - KHM
  • +237 - CMR
  • +1 - CAN
  • +1 - CYM
  • +236 - CAF
  • +235 - TCD
  • +56 - CHL
  • +86 - CHN
  • +61 - CXR
  • +61 - CCK
  • +57 - COL
  • +269 - COM
  • +682 - COK
  • +506 - CRI
  • +385 - HRV
  • +53 - CUB
  • +599 - CUW
  • +357 - CYP
  • +420 - CZE
  • +243 - COD
  • +45 - DNK
  • +253 - DJI
  • +1 - DMA
  • +1 - DOM
  • +593 - ECU
  • +20 - EGY
  • +503 - SLV
  • +240 - GNQ
  • +291 - ERI
  • +372 - EST
  • +268 - SWZ
  • +251 - ETH
  • +500 - FLK
  • +298 - FRO
  • +679 - FJI
  • +358 - FIN
  • +33 - FRA
  • +594 - GUF
  • +689 - PYF
  • +262 - ATF
  • +241 - GAB
  • +220 - GMB
  • +995 - GEO
  • +49 - DEU
  • +233 - GHA
  • +350 - GIB
  • +30 - GRC
  • +299 - GRL
  • +1 - GRD
  • +590 - GLP
  • +1 - GUM
  • +502 - GTM
  • +441481 - GGY
  • +224 - GIN
  • +245 - GNB
  • +592 - GUY
  • +509 - HTI
  • +61 - HMD
  • +504 - HND
  • +36 - HUN
  • +354 - ISL
  • +91 - IND
  • +62 - IDN
  • +98 - IRN
  • +964 - IRQ
  • +353 - IRL
  • +44 - IMN
  • +972 - ISR
  • +39 - ITA
  • +225 - CIV
  • +1 - JAM
  • +81 - JPN
  • +44 - JEY
  • +962 - JOR
  • +7 - KAZ
  • +254 - KEN
  • +686 - KIR
  • +965 - KWT
  • +996 - KGZ
  • +856 - LAO
  • +371 - LVA
  • +961 - LBN
  • +266 - LSO
  • +231 - LBR
  • +218 - LBY
  • +423 - LIE
  • +370 - LTU
  • +352 - LUX
  • +389 - MKD
  • +261 - MDG
  • +265 - MWI
  • +60 - MYS
  • +960 - MDV
  • +223 - MLI
  • +356 - MLT
  • +692 - MHL
  • +596 - MTQ
  • +222 - MRT
  • +230 - MUS
  • +262 - MYT
  • +52 - MEX
  • +691 - FSM
  • +373 - MDA
  • +377 - MCO
  • +976 - MNG
  • +382 - MNE
  • +1 - MSR
  • +212 - MAR
  • +258 - MOZ
  • +264 - NAM
  • +674 - NRU
  • +977 - NPL
  • +31 - NLD
  • +687 - NCL
  • +64 - NZL
  • +505 - NIC
  • +227 - NER
  • +234 - NGA
  • +683 - NIU
  • +672 - NFK
  • +850 - PRK
  • +1 - MNP
  • +47 - NOR
  • +968 - OMN
  • +92 - PAK
  • +680 - PLW
  • +970 - PSE
  • +507 - PAN
  • +675 - PNG
  • +595 - PRY
  • +51 - PER
  • +63 - PHL
  • +64 - PCN
  • +48 - POL
  • +351 - PRT
  • +1 - PRI
  • +974 - QAT
  • +238 - CPV
  • +242 - COG
  • +262 - REU
  • +40 - ROU
  • +7 - RUS
  • +250 - RWA
  • +378 - SMR
  • +239 - STP
  • +966 - SAU
  • +221 - SEN
  • +381 - SRB
  • +248 - SYC
  • +232 - SLE
  • +65 - SGP
  • +1 - SXM
  • +421 - SVK
  • +386 - SVN
  • +677 - SLB
  • +252 - SOM
  • +27 - ZAF
  • +500 - SGS
  • +82 - KOR
  • +211 - SSD
  • +34 - ESP
  • +94 - LKA
  • +590 - BLM
  • +290 - SHN
  • +1 - KNA
  • +1 - LCA
  • +590 - MAF
  • +508 - SPM
  • +1 - VCT
  • +249 - SDN
  • +597 - SUR
  • +47 - SJM
  • +46 - SWE
  • +41 - CHE
  • +963 - SYR
  • +886 - TWN
  • +992 - TJK
  • +255 - TZA
  • +66 - THA
  • +670 - TLS
  • +228 - TGO
  • +690 - TKL
  • +676 - TON
  • +1 - TTO
  • +216 - TUN
  • +90 - TUR
  • +993 - TKM
  • +1 - TCA
  • +688 - TUV
  • +256 - UGA
  • +380 - UKR
  • +971 - ARE
  • +44 - GBR
  • +1 - USA
  • +1 - UMI
  • +1 - VIR
  • +598 - URY
  • +998 - UZB
  • +678 - VUT
  • +39 - VAT
  • +58 - VEN
  • +84 - VNM
  • +681 - WLF
  • +212 - ESH
  • +685 - WSM
  • +967 - YEM
  • +260 - ZMB
  • +263 - ZWE
Company Name* Email* Message* Text to Identify Refresh CAPTCHA Text Verification Loading... Submit

Related Blogs

calendar February 27, 2016
Web Controller In Odoo V9

Odoo web controller provides the facility to create frontend modules which can be easily integrate with the backend modules. Here backend is refers to the modules which provides functionalities like hr, sales management, purchase management , inventory, warehouse etc. These functionalities are mostly accessed by user with permission to access these modules. Frontend modules refers odoo website functionalities like website, website sale, website blog etc. These modules creates a website pages...

views 3203 views related-blog calendar August 23, 2023
Optimizing Odoo on Kubernetes: Enhancing Deployment Efficiency with Blue-Green Strategies

In today's rapidly evolving technology landscape, businesses must be agile and responsive to meet customer demands efficiently. One critical aspect of managing software applications is deploying updates or new features seamlessly without disrupting the user experience. To achieve this, many organizations have adopted blue-green deployments, a popular deployment strategy, when running Odoo on Kubernetes. In this blog post, we will explore the concept of blue-green deployments and how they can...

views 2042 views related-blog calendar March 04, 2020
How To Process Delivery Order In 3 Steps (Pick+Pack+Ship Feature)

In any shipping business, timely delivery of goods is very important. In Odoo when an order goes to the shipping department for final delivery, Odoo is set to one-step operation by default. There are many business scenarios where the company needs to use multiple delivery steps before product shipment to the customer. This requirement can be easily met in Odoo with just a few clicks of configuration. In this blog, I will show you the configuration of the three-step delivery option (Pick +...

views 1672 views related-blog
More Question?
Let’s Talk TUV ISO 9001 Clutch clutch liferay odoo Our Presence USA usa-flag

1968 S. Coast Hwy, Suite 523, Laguna Beach, CA 92651

+1 408 914 2737 INDIA india-flag

201, 3rd Eye II, Nr. Panchvati Circle, CG Road, Ahmedabad

+91 7574805354 email[email protected] Quick Links
  • Digital Transformation Services
  • DevOps
  • Laravel Development
  • Adobe Commerce (Magento)
  • Hire Dedicated Developers
  • Hire Odoo Developers
  • Hire Liferay Developers
  • Hire AEM Developers
  • Hire Laravel Developers
  • linkedin
  • facebook
  • twitter
  • instagram
  • youtube

© 2025 SurekhaTechnologies.All rights reserved.

  • DMCA.com Protection Status
  • Privacy Policy
  • Terms of Use
Hidden We use cookies to deliver personalized content, analyze trends, administer the site, track user movements on the site, and collect demographic information about our user base as a whole. Accept all cookies for the best possible experience on our website or manage your preferences. What For? Configuration Accept All Decline All top-arrow top-arrow

Tag » Add Domain Field Odoo