This question has been flagged
5 Replies
15788 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">

Avatar
Discard
Best Answer

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:

Code:

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


Object Code:

Code:

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

Avatar
Discard
Author

thanks:)

Best Answer

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),
    }

 

Avatar
Discard
Author

thanks:)