How to hide a field on a one2many in openerp?

2k views Asked by At

I have customize the sale order menu in openerp making it into two menus named as "Local" and the other one is "Export". I add some field at sale.order class:

'is_local' : fields.boolean('Local'), #Default as true if user clicked the Local menu.
'is_export' : fields.boolean('Export'), #Default as true if user clicked the Export menu.

and at sale.order.line class:

'is_local' : fields.related('order_id','is_local',type='boolean',string='Local',store=True)
'is_export' : fields.related('order_id','is_export',type='boolean',string='Export',store=True)
'length' : fields.float('Length', digits=(12,2)),
'width' : fields.float('Width', digits=(12,2)),
'height' : fields.float('Height', digits=(12,2)),

The situation is this, if i will click the Local menu the fields: length, width, and height at sale.order.line must be hide on the view.

I did this on my sale_order view:

<field name="order_line" context="{'default_is_local': local}"/>
    <tree string="Order line">
        <field name="is_local"/>
        <field name="length" invisible="context.get('is_local',True)"/>
    .
    .
    .
</field>

Using invisible="context.get('is_local',True)", its problem that is it also hide the field if i choose the export menu w/c is not supposed to be hide. When i use attrs="{'invisible':[('is_local','=',True)]}" it doesn't hide the field both on Local and Export Menu. I don't know what technique is to be use.

I am using openerp v7

Any help is much appreciated!

====================================== EDIT

<record id="view_order_form_inherit" model="ir.ui.view">
<field name="name">biz1.view.order.sale</field>
<field name="model">sale.order</field>
<field name="inherit_id" ref="sale.view_order_form" />
<field name="priority">1</field>
<field name="arch" type="xml">
    <xpath expr="//field[@name='partner_id']" position="after">
        <field name="is_export" attrs="{'invisible':[('is_export','=',False)]}" />
        <field name="is_local" attrs="{'invisible':[('is_local','=',False)]}" />
    </xpath>
    <xpath expr="//field[@name='order_line']" position="replace">
        <field name="order_line" context="{'default_is_local':is_local}"
            colspan="4" nolabel="1" widget="one2many_list">
            <tree string="Order Line" editable="bottom" />
            <field name="sequence" widget="handle" />
            <field name="state" invisible="1" />
            <field name="th_weight" invisible="1" />
            <field name="is_local" />
            <field name="product_id"
                context="{'partner_id':parent.partner_id, 'quantity':product_uom_qty, 'pricelist':parent.pricelist_id, 'shop':parent.shop_id, 'uom':product_uom}"
                groups="base.group_user"
                on_change="product_id_change(parent.pricelist_id, product_id, product_uom_qty, product_uom, product_uos_qty, product_uos, name, parent.partner_id, False, True, parent.date_order, False, parent.fiscal_position, False, context)" />
            <field name="name" />
            <field name="product_uom_qty"
                context="{'partner_id':parent.partner_id, 'quantity':product_uom_qty, 'pricelist':parent.pricelist_id, 'shop':parent.shop_id, 'uom':product_uom}"
                on_change="product_id_change(parent.pricelist_id, product_id, product_uom_qty, product_uom, product_uos_qty, product_uos, name, parent.partner_id, False, False, parent.date_order, False, parent.fiscal_position, True, context)" />
            <field name="product_uom"
                on_change="product_uom_change(parent.pricelist_id, product_id, product_uom_qty, product_uom, product_uos_qty, product_uos, name, parent.partner_id, False, False, parent.date_order, context)"
                groups="product.group_uom" options='{"no_open": True}' />
            <field name="product_uos_qty" groups="product.group_uos"
                invisible="1" />
            <field name="product_uos" string="UoS" groups="product.group_uos"
                invisible="1" />
            <field name="tax_id" widget="many2many_tags"
                domain="[('parent_id','=',False),('type_tax_use','&lt;&gt;','purchase')]" />
            <field name="price_unit" />
            <field name="length" invisible="context.get('is_local',True)"  />
            <field name="width" invisible="context.get('is_local',True)"   />
            <field name="height" invisible="context.get('is_local',True)"  />
            <field name="discount" groups="sale.group_discount_per_so_line" />
            <field name="price_subtotal" />
        </tree>
        </field>
    </xpath>
</field></record>


<!-- ##### LOCAL SO #### -->

<record id="action_local_sale_orders" model="ir.actions.act_window">
<field name="name">Local SO</field>
<field name="res_model">sale.order</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form,calendar,graph</field>
<field name="view_id" ref="view_order_form_inherit" />
<field name="context">
    {'default_is_local':'1'}
</field>
<field name="domain">[('is_local','=','1')]</field>
<field name="help" type="html">
    <p class="oe_view_nocontent_create">
        Click to create a quotation that can be converted into a sales order.
     </p>
    <p>
        OpenERP will help you efficiently handle the complete sales flow: quotation,
        sales order, delivery, invoicing and payment.
    </p>
</field>
</record>

<menuitem action="action_local_sale_orders" name="Local"
id="menu_local_sale_order" parent="base.menu_sales" sequence="6"
groups="base.group_sale_salesman,base.group_sale_manager" />


<!-- ##### EXPORT SO #### -->

<record id="action_export_sale_orders" model="ir.actions.act_window">
<field name="name">Export SO</field>
<field name="res_model">sale.order</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form,calendar,graph</field>
<field name="view_id" ref="view_order_form_inherit" />
<field name="context">
    {'default_is_export':'1'}
</field>
<field name="domain">[('is_export','=','1')]</field>
<field name="help" type="html">
    <p class="oe_view_nocontent_create">
        Click to create a quotation that can be converted into a sales order.
     </p>
    <p>
        OpenERP will help you efficiently handle the complete sales flow: quotation,
        sales order, delivery, invoicing and payment.
    </p>
</field>
</record>

<menuitem action="action_export_sale_orders" name="Export"
id="menu_export_sale_order" parent="base.menu_sales" sequence="6"
groups="base.group_sale_salesman,base.group_sale_manager" />
1

There are 1 answers

8
Emipro Technologies Pvt. Ltd. On BEST ANSWER

Define actions for the menu on which you want to show/hide fields.

<record id="action_id" model="ir.actions.act_window">
    <field name="name">Name</field>
    <field name="res_model">Model</field>
    <field name="type">ir.actions.act_window</field>
    <field name="view_type">form</field>
    <field name="view_mode">form</field>
    <field name="context">{'is_local' : True}</field>
</record>

And assign that action to the menu. and then your code is working fine.

The reason you are not getting proper output is that, related fields are always gives value once your related records has been stored. in your case if order is not created then what it gives in is_local and is_export ?

So, you need to pass that value through the context via action.

Try this and let me know whether you will get solution or not.

Edited your code here.

Replace this portion in action of Export SO.

<field name="context">
    {'default_is_export':'1','is_local':False}
</field>

Replace this portion in action of Local SO.

<field name="context">
    {'is_local':True}
</field>

And replace this fields in view.

<field name="length" invisible="context.get('is_local',False)"  />
<field name="width" invisible="context.get('is_local',False)"   />
<field name="height" invisible="context.get('is_local',False)"  />