This question has been flagged
7011 Views

For form views, I was able to specify context for a many2one field for name_get and name_search, however, I cannot pass context to list views since the <fields> do not take context="" as an attribute. How do I work around this?

In detail, I have a Shipment and Order class:

class Shipment(models.Model):
    _name = "custom_shipment"
    _rec_name = "shipment_name"
    order_id = fields.Many2one('custom_order')
    shipment_name = fields.Char(string = "Shipment Identifier")
class Order(models.Model):
    _name = "custom_order"
    _rec_name = "order_name"
    order_name = fields.Char(string = "Order Identifier")

    @api.multi
    def name_get(self):
         special_switch = self._context.get('special_switch','')
         if(special_switch == '1'):
             #do something special
         elif(special_switch == '2'):
             #do something else
         else:
             #do something else again
         return result

As well as views to display Shipments:

<record model="ir.ui.view" id="custom_shipment_form">
    <field name="name">custom_shipment.form</field>
    <field name="model">custom_shipment</field>
    <field name="arch" type="xml">
        <form>
             <sheet>
                <field name="order_id" context="{'special_switch':context.get('special_switch')}"/>
                <field name="shipment_name"/>
             </sheet>
        </form>
    </field>
</record>
<record model="ir.ui.view" id="custom_shipment_list">
    <field name="name">custom_shipment.tree</field>
    <field name="model">custom_shipment</field>
    <field name="arch" type="xml">
        <tree>
            <field name="order_id"/>
            <field name="shipment_name"/>
        </tree>
    </field>
</record>

 In the form view, the context is passed, and the correct name is generated, however specify the same thing for order_id field in tree/list view and the context is not passed as per the documentation. Is there a work around?

Avatar
Discard