odooodoo-14

How to allow/disallow create and delete on One2Many field tree?


Odoo 14 Community Edition

I have a One2Many field called lines. I display the field as a tree in a form view like this.

<field name="lines">
    <tree editable="bottom">
        <field 
         ... doesn't matter ...
        />
    </tree>
</field>

The field is now editable but I would like to change it so that it will allow to create/delete the records within the tree by some conditions.

I tried create="0" delete="0" in the tree but it only works for "0" and "1". I can't seem to find the way to put a condition in there.

How to overcome such challenge? The conditions can just be depending on the parent's state -- simple conditions.


Solution

  • You can use attrs on the field itself, because besides the often used invisible attribute you also can use readonlyand required.

    Using readonly with a domain on the field will lead to other usable fields. Because the field lines is a x2many relation (child) so the fields of the parent are usable.

    Because i don't know the models here, let's assume we have the known one2many relation sale.order and sale.order.line and we want the lines to get readonly when the state field has either the value sale or done.

    <field name="order_line" attrs="{'readonly': [('state', 'in', ['sale', 'done'])]}">
        <tree>...</tree>
    </field>