Dayana
Dayana

Reputation: 1538

How to make all fields readonly in a form when the state is 'confirmed'?

When the state of an object changes to 'confirmed', I need all fields in the form to be readonly. So far I put attrs = "{'readonly': [('state', '=', 'confirmed')]}" in each field, but I would like to know if there is any way to make it more optimal.

Upvotes: 3

Views: 2057

Answers (1)

forvas
forvas

Reputation: 10189

If you want to apply that condition to every view of the model (there are models with more than one form view displayed in different parts of Odoo), it would be better to specify it in Python. In the definition of each field, you should add states argument:

your_field = fields.Whatever(
    ...
    readonly=False,
    states={
        'confirmed': [('readonly', True)],
    }
)

This way, if the model is opened by the user through a view different from the one you have modified, the fields would be readonly if the state is confirmed, it doesn't matter that you haven't modified the opened view.

On the other hand, if you want to apply your purpose only in a specific form view, you could do something quicker than adding the attrs to each field, and it would be adding it to tags which contain several fields, like group, for example. This also works and is faster for you:

<group attrs="{'readonly': [('state', '=', 'confirmed')]}">
    <field name="your field_1"/>
    <field name="your field_2"/>
    ...
</group>

Upvotes: 3

Related Questions