Skip to Content
Menu
Dette spørgsmål er blevet anmeldt
6 Besvarelser
42858 Visninger

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
Kassér
Forfatter Bedste svar

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
Kassér
Bedste svar

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
Kassér
Forfatter

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

Forfatter

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

Bedste svar

Hi, Pascal Tremblay

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

Avatar
Kassér
Forfatter

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.

Related Posts Besvarelser Visninger Aktivitet
1
mar. 15
4462
0
mar. 24
3893
1
mar. 24
30702
1
okt. 15
5616
1
jul. 15
3663