Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
6 Odpowiedzi
42866 Widoki

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')]}" />
Awatar
Odrzuć
Autor Najlepsza odpowiedź

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)]}"/>
Awatar
Odrzuć
Najlepsza odpowiedź

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">
            .....

Awatar
Odrzuć
Autor

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

Autor

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

Najlepsza odpowiedź

Hi, Pascal Tremblay

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

Awatar
Odrzuć
Autor

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.

Powiązane posty Odpowiedzi Widoki Czynność
1
mar 15
4463
0
mar 24
3903
1
mar 24
30713
1
paź 15
5619
1
lip 15
3669