This question has been flagged

I have a Many2one field already works in a lot of records in a production instance, now is needed that this field accepts more than 1 relation link to the other model, is possible just change the field type from Many2one to Many2many without losing any data?

If yes, what's the correct way to do it? if not, how to do it?

Avatar
Discard

Why would you want to change from a many2one to a many2many though?You're most likely better off to create a one2many that links to a many2one - it'll behave the same as a many2many but it has benefits. On a many2many you cannot filter by default and if you export the data you won't get everything out automatically. With a one2many you can. Just a tip. :-)

Author

@Yenthe Your approach it's helpful, I've not even though, so, can I take your answer as If I can change the field from Many2one to One2many or Many2many and the data persist?

Author

I can't use the One2many field because 1 a.object can have N b.object and 1 b.object can be in N a.object, so I should use the Many2many field, but I need to know if the information persists after changing the field type from one2Many to (Many2many or One2many).

Author Best Answer

I did it this way:

first I created a new field type Many2many:

objectA_ids = fields.Many2many('res.partner')

then a function that gets the value of the old Many2one field:

@api.model
    def _fill_objects(self):
        products = self.env['module.name'].search(
            ['&', ('objectA_id', '!=', False), ('objectA_ids', '=', False)])
        for r in products:
            if not r.objectA_ids and r.objectA_id:
                r.objectA_ids = [(4, r.objectA_id.id)]
            else:
                r.objectA_ids = False


and in the last step, i did make an XML call to the function when the module it's installing:


<odoo>
   <data noupdate="1">
    <function model="module.name" name="_fill_objects"/>
  </data>
</odoo>


after installing the module the new field has the values of the old field, there's no way for change the field type without lost data, so we have to create a new field, copy data from the old field, and make the old field invisible on view.


<odoo>
  <data>
    <record id="view_module_name_adjustment" model="ir.ui.view">
      <field name="name">module.name.view.form</field>
      <field name="model">module.name</field>
      <field name="inherit_id" ref="module_view_external_id"/>
      <field name="arch" type="xml">
        <field name="objectA_id" position="after">
          <field name="objectA_ids" widget="many2many_tags"/>
        </field>
        <field name="objectA_id" position="attributes">
          <attribute name="invisible">True</attribute>
        </field>
      </field>
    </record>
   </data>
</odoo>
Avatar
Discard