odoo context field. default value for popup

Danila Ganchar picture Danila Ganchar · Jan 21, 2016 · Viewed 23.8k times · Source

I work with Odoo(v9). I have custom: form(for model 1), action and popup with form(for model 2). Here example main form:

<record id="my_id_form" model="ir.ui.view">
    <field name="name">my_name_form</field>
    <field name="model">my_model_one</field>
    <field name="arch" type="xml">
        <form string="Name">
            <sheet>
                <group>
                    <field name="partner_id"/>
                    <!-- button which open popup with my_model_two -->
                    <button string="Open popup"
                            name="%(my_module.action_open_popup)d"
                            type="action"
                            class="btn-link"/>
                </group>
            </sheet>
        </form>
    </field>
</record>

Window action for button Open popup:

<record id="action_open_popup" model="ir.actions.act_window">
    <field name="name">action name</field>
    <field name="res_model">my_model_two</field>
    <field name="view_id" ref="model_two_form_popup"/>
    <!-- 
    How I can send partner_id from main form to popup? 
    I tried different ways in context field, but all in vain  
    <field name="context">{'default_partner_id': ?????,}</field>
    -->
    <field name="target">new</field>
</record>

Example my_model_one

class MyModelOne(models.Model):
    _name = 'my_model_one'

    partner_id = fields.Many2one('res.partner', string='Partner')

Popup form:

<record id="model_two_form_popup" model="ir.ui.view">
    <field name="name">Popup name</field>
    <field name="model">my_model_two</field>
    <field name="arch" type="xml">
        <form string="Popup text">
            <sheet>
                <group>
                    <field name="partner_id" invisible="1"/>
                <group>
            </sheet>
        </form>
    </field>
</record>

My question is: How I can send value from field in main form to popup form?(partner_id)

I saw how in the code used active_id, string or integer values. But I have not found how to send fields values or how to register method for custom logic. Can someone provide a small example? Thanks in advance.

Answer

Danila Ganchar picture Danila Ganchar · Jan 21, 2016

I found the solution. In element button need to add context like this:

<button string="Open popup"
    name="%(my_module.action_open_popup)d"
    type="action"
    class="btn-link"
    <!-- name_of_parameter: name_of_field -->
    context="{'partner_id': partner_id}"/>

After this we need set default value to popup:

<record id="action_open_popup" model="ir.actions.act_window">
    <field name="name">action name</field>
    <field name="res_model">my_model_two</field>
    <field name="view_id" ref="model_two_form_popup"/>
    <!-- set default value to field from context parameter by name -->
    <field name="context">{'default_partner_id': context.get('partner_id', False),}</field>
    <field name="target">new</field>
</record>