This question has been flagged
6 Replies
38836 Views

Hello there,

Let me explain with an example.

In the sale order model, we have these two fields : note (text) and picking_ids (many2many)

In the form view of the sale order, I would want the field note readonly if all the associated pickings of the sale order are in "confirmed" state.

Actually, the note field is displayed like it in the sale order form view :

<field name="note" class="oe_inline"/>

How could I manage it?


EDIT #1

I would want something like that. But it doesn't work.  :

<field name="note" class="oe_inline" attrs="{'readonly': [('picking_ids.state', '=', 'confirmed')]}" />
Avatar
Discard
Author Best Answer

Here is our solution for the above example.

Thanks all for your help.


I have defined a new boolean field in the sale.order model:

all_picking_confirmed = fields.Boolean(compute='compute_all_picking_confirmed')

With his function :

def compute_all_picking_confirmed(self):
        for order in self:
            order.all_picking_confirmed = True           
                if order.picking_ids:
                    for picking in order.picking_ids:
                        if picking.state not in ('done'):
                            order.all_picking_confirmed = False
                else:
                    order.all_picking_confirmed = False

And in the view :

<field name="note" attrs="{'readonly':[('all_picking_confirmed','=',True)]}"/>
Avatar
Discard
Best Answer

Salut Pascal,

You have a pretty good example in the OCA (probably many other place but first to came to my mind) partner_contact/partner_firstname, togling the read only if it's not a company

<xpath expr="//field[@name='name']" position="attributes">

                    <attribute name="attrs">{

                        'readonly': [('is_company', '=', False)],

                        'required': [('is_company', '=', True)]

                        }</attribute></xpath>


If it's not a boolean, but a matter of list of items, you can go straight to the if variableliste as per OCA/website_sale demonstrate in in their first view declaration

<t t-if="not quotations">

            <p>There are currently no quotes for your account.</p>

        </t>

        <t t-if="quotations">

            <div class="table-responsive">
            .....

Avatar
Discard
Author

In your example, the field is_company is a boolean. In my example, picking_ids is a many2many field. Don't think they work the same way. More, I need to check in picking_ids.state... Thanks

Author

Are you really sure that we can use t-if condition in a form view of the backend? I don't manage

Best Answer

Hi, Pascal Tremblay

     You can use (readonly="1") in field.

Avatar
Discard
Author

I know this. But the field "note" must be readonly ONLY if ALL the pickings (many2many, picking_ids) of the sale order are in "confirmed" state. You give me a way to make the field ALWAYS readonly.