Перейти к содержимому
Меню
Чтобы взаимодействовать с сообществом, необходимо зарегистрироваться.
Этот вопрос был отмечен
6 Ответы
42917 Представления

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')]}" />
Аватар
Отменить
Автор Лучший ответ

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)]}"/>
Аватар
Отменить
Лучший ответ

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

Аватар
Отменить
Автор

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

Автор

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

Лучший ответ

Hi, Pascal Tremblay

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

Аватар
Отменить
Автор

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 Ответы Просмотры Активность
1
мар. 15
4468
0
мар. 24
3928
1
мар. 24
30731
1
окт. 15
5627
1
июл. 15
3685