Skip to Content
Menu
This question has been flagged
3 Replies
19317 Views

Hi,

Is it possible to use a computed field to make a particular field invisible based on a condition?

is it possible using compute field?
If the answer is yes, how can this effect be visible in the view?

Please help me

Avatar
Discard
Best Answer

@chirra

You could use it like a normal field, for example:

<field name="make_invisible" invisible="1"/>
<field name="field_x" attrs="{'invisible':[('make_invisible', '=', True)]}"/>

Just note that the fields functions just compute it's value when the form is saved. You could assign a default_value for the function field or change it's value using an onchange.

Avatar
Discard
Author

Hi Axel Mendoza i need invisible the filed through python code only for field level security my issue like this: . When we want to invisible a field conditionally in same group, we using domains to “attrs”. Using browser inspect element feature we can simply remove “oe_invisible” css class and remove the invisible logic.

then change the view by override of the method fields_view_get and deal with xml nodes

Best Answer

Hi,

I think you may use a compute boolean field, that becomes true or false based on the condition. You can make another field visible or invisible using the "attrs" attribute, based on that boolean field. You may note that onchange is default on compute fields and it also compute the values, when you open or edit in form view.

For example, you may see the following example, in that, if the value of field1 is "Any_String", the field2 will be invisible.

in .py file:

class test_model(models.Model):

_name = "test.model"


field1 = fields.Many2one('another.model', String="First field")

check = fields.Boolean(compute='_get_value')

field2 = fields.Char(String="Second field")


@api.one

@api.depends('field1')

def _get_value(self):

if self.field1.name == "Any_String":

self.check = True

else:

self.check = False

in xml file:

    <record model="ir.ui.view" id="test_model_form_view">

<field name="name">test.model.form.view</field>

<field name="model">test.model</field>

<field name="arch" type="xml">

                 <group>

                    <field name="field1" />

                    <field name="check" invisible="1" />

                    <field name="field2" attrs="{'invisible':[('check', '=', True)]}"


</field>

</record>

Avatar
Discard