Odoo Help


what is CONTEXT?

on 7/30/14, 2:25 AM 2,307 views

Does anybody here knows what is the context and what is the use of this??


<field colspan="4" name="commit_line_ids" nolabel="1" widget="one2many_list" mode="tree" context="{}" readonly="False">

On 7/30/14, 3:59 AM

Context is used to pass arbitrary contextual information to the server.

For example, on a one2many field on the partner object, you may put context="{'partner_id' : active_id}". This information is then available to the server when generating the forms that pop up from the one2many field, for example in '_defaults' field functions. However the object is not (necessarily) filtered by anything you put into the context, only if the object designer has decided that it should be.

One neat little trick actually is to use the context to pass in default values for fields. E.g. to pass in a default value for a 'name' field, use: context="('default_name' : 'David')"

Another use for Context is on buttons. You can use it to pass parameters to object functions. E.g.:

View XML:


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

Object Code:


def my_func(self, cr, uid, ids, context={}):
    # do stuff using the info passed in the context variable...


on 7/30/14, 4:29 AM
On 7/30/14, 3:44 AM

You can set the context parent value in onemany field in xml file and get the context value in python in default method or on_change method.

For example,

_name = "sample.order"

_columns = {

'partner_id': fields.many2one('res.partner', 'Customer'),

'order_line': fields.one2many('sample.order.line', 'order_id', 'Order Lines', readonly=True),


<field name="order_line"  context="{'partner_id':parent.partner_id}" nolabel="1" colspan="4">

_name = "sample.order.line"

  _columns = {

'order_partner_id': fields.many2one('res.partner', 'Customer'),


_defaults = {
        'order_partner_id' : context.get('partner_id', False),



on 7/30/14, 4:29 AM

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)


Asked: 7/30/14, 2:25 AM
Seen: 2307 times
Last updated: 8/5/15, 1:35 PM