Saturday, September 29, 2012

context and domain


Domain is used purely for filtering (e.g. on Menu Actions, to control the records that are available in the tree/list and form views)

Context, as its name suggests, 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' : 'Fred')"

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...

1 comment:

  1. Initially contex and domain, for me, seems both similar to a filter; so context are only a way to pass a list of parameters, same as the call of function.

    ReplyDelete