0

I tired to copy attachment from one2many field to another one2many based on id. Does write command work ??? The following code doesn't work properly.

existing_documents = self.env['document.product.verify'].search([('docs_id', '=', self.partner_id.id)])
for record1 in self.product_line:
    for record2 in existing_documents:
        if record1.documents_required_id.id == record2.documents_required_id.id:
            print '*****************DOCUMENT EXISTS******************'
            record1.write({'attachments': record2.attachments})
            print '***************records***************', record1.id, record2.id
            print record1.attachments, record2.attachments
NinjaBat
  • 370
  • 4
  • 20

2 Answers2

3

Use this to Update/Insert/Delete data of one2many field :

(0, 0, { values }) link to a new record that needs to be created with the given values dictionary

(1, ID, { values }) update the linked record with id = ID (write values on it)

(2, ID) remove and delete the linked record with id = ID (calls unlink on ID, that will delete the object completely, and the link to it as well)

(3, ID) cut the link to the linked record with id = ID (delete the relationship between the two objects but does not delete the target object itself)

(4, ID) link to existing record with id = ID (adds a relationship)

(5) unlink all (like using (3,ID) for all linked records)

(6, 0, [IDs]) replace the list of linked IDs (like using (5) then (4,ID) for each ID in the list of IDs)

Nitin Kantak
  • 168
  • 9
0

As Nitin sugested, the following code worked.

    existing_documents = self.env['document.product.verify'].search([('docs_id', '=', self.partner_id.id)])
    for record1 in self.product_line:
        for record2 in existing_documents:
            if record1.documents_required_id.id == record2.documents_required_id.id:
                hoh1 = (0, 0, {'documents_required_id': record1.documents_required_id.id,
                               'attachments': record2.attachments,
                               'file_name': record2.file_name})
                result_new.append(hoh1)
                break
            else:
                hoh1 = (0, 0, {'documents_required_id': record1.documents_required_id.id})
                result_new.append(hoh1)
                break
    self.update({'product_line': result_new})
NinjaBat
  • 370
  • 4
  • 20