odooodoo-11

How to copy all fields's value from one record into another?


I have a existing record A and I want cover its value from record B. In regular way, we use A.write({'filed1':RecB's_value,...}) to update. But the number of fields near 400, I was wonder that if there is a convenient way to do that.

I have tried that as code below

copy = rec_b.copy()
rec_a.write({copy})

got error

AttributeError: 'yc.purchase' object has no attribute 'pop'

I expected that can instead my manual assignment.


Solution

  • I found solution, hope it can help who have same issue.

    a = self.browse(a)
    b = self.browse(b)
    # read all field_name into a list
    _fields = []
    for fn in self._proper_fields._map.keys():
        _fields.append(fn)
    # start to write
    vals = {}
    for _f in _fields:
        # M2O must use id
        if hasattr(b[_f],'id'):
            # a.write({_f: b[_f].id})
            vals.update({_f: b[_f].id})
        else:
            # a.write({_f: b[_f]})
            vals.update({_f: b[_f]})
    
    a.write(vals)