What's the context?

Several questions about context in OpenERP:

  1. What is it ?
  2. How to define it and what is active_id regarding context ?
  3. What does context.get('Active_id',False/True) stand for ?

The context is a python dictionary and is used to pass certain data to a method. Since nearly all methods have a context parameter you can use the context to pass data through several levels of python methods. For example you can set a context value in a XML-view and process it in the write() method of the osv object.

 context.get('active_id',False)

returns the value of the key 'active_id'. If the key is not in the context, then it returns the value 'False'.

The value in 'active_id' is passed from the web client and contains the ID of the current selected record.

Here is an example how the context is used in the Sale module. File sale_view.xml:

        <record id="view_order_form" model="ir.ui.view">
            <field name="name">sale.order.form</field>
            <field name="model">sale.order</field>
            <field name="arch" type="xml">
                <form string="Sales Order" version="7.0">
                    <header>
                        <button name="invoice_recreate" states="invoice_except" string="Recreate Invoice" groups="base.group_user"/>
                        <button name="invoice_corrected" states="invoice_except" string="Ignore Exception" groups="base.group_user"/>
                        <button name="action_quotation_send" string="Send by Email" type="object" states="draft" class="oe_highlight" groups="base.group_user"/>
....

You can see that the button third button (Send by Email) calls method action_quotation_send in sale.py.

Here is the relevant code in sale.py:

def action_quotation_send(self, cr, uid, ids, context=None):
    .......
    ctx.update({
        'default_model': 'sale.order',
        'default_res_id': ids[0],
        'default_use_template': bool(template_id),
        'default_template_id': template_id,
        'default_composition_mode': 'comment',
        'mark_so_as_sent': True
    })
    return {
        'type': 'ir.actions.act_window',
        'view_type': 'form',
        'view_mode': 'form',
        'res_model': 'mail.compose.message',
        'views': [(compose_form_id, 'form')],
        'view_id': compose_form_id,
        'target': 'new',
        'context': ctx,
    }

The context is updated with the flag mark_so_as_sent set to True. Afterwards the send-mail window (mail.compose.message) is opened and the modified context is passed.

Then the send_mail() method is overwritten. If the flag mark_so_as_sent has been set in the context, then the new functionality is called. This ensures that the current behavior of send_mail() is not affected. But when the wizard is opened from the Sale Order, the new functionality will be executed.

Also in sale.py:

class mail_compose_message(osv.Model):
    _inherit = 'mail.compose.message'

    def send_mail(self, cr, uid, ids, context=None):
        context = context or {}
        if context.get('default_model') == 'sale.order' and context.get('default_res_id') and context.get('mark_so_as_sent'):
            context = dict(context, mail_post_autofollow=True)
            wf_service = netsvc.LocalService("workflow")
            wf_service.trg_validate(uid, 'sale.order', context['default_res_id'], 'quotation_sent', cr)
        return super(mail_compose_message, self).send_mail(cr, uid, ids, context=context)

Need more info?

This documentation page has been extracted from the Q&A section where you can discuss it and get feedback.
Related question