1

I have the following wizard structure [ field name and data type ]

  • Type :- Selection :- Type 1 and Type 2
  • Route :- One2many

When Users select Type 1, I want to allow them to add records in the Route table. While on the Type 2, I want to make Route readonly and don't allow deletion. I will fill it with default route information.

I write following code in the .xml file:

<group attrs="{'invisible': [('type', '=', 'type_2')]}">
    <field name="route_ids" string="Testing 1">
        <tree>
            <field name="x"/>
            <field name="y"/>
        </tree>
    </field>
</group>

<group attrs="{'invisible': [('type', '=', 'type_1')]}">
    <field name="route_ids" string="Testing 2">
        <tree delete="false" create="false">
            <field name="x"/>
            <field name="y"/>
        </tree>
    </field>
</group>

I notice that based on Type selection, route field label is changing but tree attributes (readonly, delete) remain same / whatever set in the last.

Expectation:

One2many field attribute should be refreshed instead of keeping last.

I resolved it by adding a new field and onchange method but I'm looking for a better approach to resolve it.

Bhavesh Odedra
  • 10,990
  • 12
  • 33
  • 58

3 Answers3

1

You can use the web_action_conditionable module, it adds support for conditions on create and delete actions on One2Many fields.

Example:

<field name="route_ids">
    <tree delete="type=='type_2'" create="type=='type_2'">
        <field name="x"/>
        <field name="y"/>
    </tree>
</field>
Kenly
  • 24,317
  • 7
  • 44
  • 60
  • Thank you Kenly. It makes sense. I will go with my own solution because I don't want to add extra module dependency in my project. [ I haven't found any Odoo core attributes solution to achieve it. ] – Bhavesh Odedra Dec 05 '22 at 23:36
0
  1. Have you tried to make your delete-attribute dynamic:
    <tree t-att-delete="'false' if type=='type_1' else 'true'" >
    ...
sylvain
  • 853
  • 1
  • 7
  • 20
  • Thanks for your answer. This will not work. Review my code in the question, before one2many field, I have a group and it is toggle based on type. For a type 1, I want to display it at 2nd block while for type 2, at the end all fields. – Bhavesh Odedra Nov 28 '22 at 20:19
  • So I have to go with 2 times field declaration and apply different attributes. – Bhavesh Odedra Nov 28 '22 at 20:20
  • yup, I tried it different ways and didn't work. - keep display one field and apply code, - keep display two field and apply code. – Bhavesh Odedra Nov 28 '22 at 20:25
  • How about adding an empty before your second tree to differenciate tree[2] from tree[1] ? – sylvain Nov 28 '22 at 20:28
  • I added empty tree tag as per your suggestion and didn't work. – Bhavesh Odedra Nov 28 '22 at 20:34
  • How about using js or jquery ressource with an $(".type_class").onchange() that will add the DELETE button inside each tr-nodes : $('tr').append('') – sylvain Nov 28 '22 at 20:56
  • I am not good in js so IMO adding a new field is simple enough. I am trying to find Odoo core attributes without making more complex. I reported issue to Odoo because this is straight forward requirement. Thanks for your help on it. – Bhavesh Odedra Nov 28 '22 at 21:05
0

According to the problem, I have two solution:

  1. Adding a new One2many, fill data via onchange method

    printing_route_ids = fields.One2many(
        string='Printing Route',
        comodel_name='x.y.z.wizard',
        inverse_name='printing_x_y_z_id',     # printing_x_y_z_id is a M2O in a line level table
    )
    

    I have another O2M field called "QC Route" which is editable. So transfer all QC routes to the Printing route and from Printing Route, I updated the final Route field so I do need to write custom logic to manage different routes. In my case, I have multiple types so route_ids field is very important.

    @api.onchange('qc_route_ids')
    def onchange_qc_route_ids(self):
     if self.type == "type_1":
         self.printing_route_ids = False
         mrp_workcenter_lines = []
         # My other logic
    
         # Prepare qc route
         for line in self.qc_route_ids:
             qc_vals = {
                 'operation': line.operation,
                 'workcenter_id': line.workcenter_id.id,
                 'sequence': line.sequence,
                 'note': line.note,
                 'worksheet': line.worksheet,
             }
             mrp_workcenter_lines.append((0, 0, qc_vals))
         self.printing_route_ids = mrp_workcenter_lines
    

    On the done button click, I transfer all values to the final Route.

    def button_done(self):
        if self.type == "type_1":
            self.route_ids = self.printing_route_ids
        # leave all logic same with changing route_ids
        return True
    

    And xml side, for the second group, I replaced field from "route_ids" to "printing_route_ids"

  2. @Kenly's answer. Add dependency of module web_action_conditionable and apply condition to one2many tree view.

Bhavesh Odedra
  • 10,990
  • 12
  • 33
  • 58