Kiran
Kiran

Reputation: 1531

How to hide fields based on condition in odoo?

I have created few custom fields for products.Products appear in sales,purchase,warehouse and manufacturing module.I want to make my custom fields appear only in manufacturing module and hide everywhere else.So how to put condition on invisible attribute.I tried like this and got error as Unknown field _name in domain

attrs="{'invisible': [('_name', '!=', 'mrp.bom')]}"

Python file,

from openerp import fields,models, api

class product_template(models.Model):
    _inherit = "product.template"

    rim_weight = fields.Float(string='Rim Weight(KG)', readonly=True, compute='_compute_one_rim_weight')
    width = fields.Float(string='Width(cm)',default='50')
    length = fields.Float(string='Length(cm)',default='63')
    gsm = fields.Float(string='Gram per square meter(gsm)',default='230')

Xml file,

<record id="product_template_form_view_dis_inherit" model="ir.ui.view">
    <field name="name">product.template.common.form.dis.inherit</field>
    <field name="model">product.template</field>
    <field name="inherit_id" ref="product.product_template_form_view"/>
    <field name="arch" type="xml">
         <xpath expr="//page[@string='Accounting']" position='after'>
            <page string='Cover Page Paper'>
                <group>
                    <field name="width"/>
                    <field name="length"/>
                    <field name="gsm"/>
                    <field name="rim_weight"/>
                </group>
            </page>
         </xpath>
    </field>
</record>

Upvotes: 4

Views: 6597

Answers (1)

There are many ways to do this, however I suggest following options to you.

  1. Modify an existing action and set context inside that and based on that context just write condition in view. (Remember here you need to override an action, and if you want to create another then you need to redefine menu to assign new action to that menu).

  2. Create new view and set this view reference in an action of that model in which you want to show these fields. In new view you need to visible those fields, no need to extend product template existing view.

However the 1st solution is easy to achieve and 2nd one is lengthy.

Example of 1st solution:

<record id="action_name" model="ir.actions.act_window">
    <field name="name">Name</field>
    <field name="res_model">product.template</field>
    <field name="view_type">form</field>
    <field name="view_mode">tree,form</field>
    <field name="context" eval="{'is_manufacturing_model':True}" />
</record>

And then in view just write like this

<page string='Cover Page Paper'>
    <group invisible="context.get('is_manufacturing_model',False)">
        <field name="width"/>
        <field name="length"/>
        <field name="gsm"/>
        <field name="rim_weight"/>
    <group>
</page>

Upvotes: 6

Related Questions