Prevent create and delete on a One2many tree view

I have this One2many field displayed as a tree view, and I want to prevent creation and deletion of rows conditionally, but also leave the existing rows editable, so readonly wont work for me, as it hide the the add an item button and trash icon but make the field non editable.

I tried attrs="{'create': [('order_type', '!=', 'renewal)]}" on the tree tag, same for delete, but it did not work.

is it possible to do that form xml, without having to write some JS code ?

thank you in advance!

Upvotes: 0

Views: 3705

Answers (1)

Alonso
Alonso

Reputation: 121

I handle this using 2 fields. In your model add another reference to the same records:

editable_lines_ids = fields.One2many(
    'reference.to.model',
    'field_id'
)
no_editable_lines_ids = fields.One2many(
    'reference.to.model',
    'field_id'
)

Then, in the view add the two fields but with conditionally visibility:

<field
    name="editable_lines_ids"
    mode="tree"
    attrs="{'invisible': [('order_type', '!=', 'renewal)]}">
    <tree
      string="Lines"
      create="1"
      editable="bottom"
      delete="1">
        <field name="example_field"/>
    </tree>
</field>
<field
    name="no_editable_lines_ids"
    mode="tree"
    attrs="{'invisible': [('order_type', '=', 'renewal)]}">
    <tree
      string="Lines"
      create="0"
      editable="bottom"
      delete="0">
        <field name="example_field"/>
    </tree>
</field>

Upvotes: 2

Related Questions