Search code examples
python-2.7odooodoo-8openerp-8

Change type of a model field in Odoo preserving old records


I have model classes:

class Video(models.Model):
    _name = 'my.video'

    category_id = fields.Many2one('my.category')
    # fields

class Category(models.Model):
    _name = 'my.category'

    # fields

Now in Video model I want to change the relationship

category_id = fields.Many2one('my.category')

to

category_id = fields.Many2many('my.category')

But the problem is I have a live project. So there are records in Video model with category assigned to each video. I cannot loose these records and I will need old categories set for videos in new Many2many relation. So what is the best way to replace the field while preserving the previous records in a way these records are usable with the new design (relation) too. Or how can I migrate records from previous field to new field?


Solution

  • Hi Muhammad Tahir Butt,

    Create a New field named :

    category_ids = fields.Many2many('my.category')
    

    Now write some external script using XMLRPC Client lib and just move values from category_id to category_ids. this way you will preserver past values on old field and then by copying values old field to new field you will have field with existing record.

    Bests