Search code examples
xmlodooodoo-13xml-attribute

one2many field view - attribute


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.


Solution

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