2
votes

I have two one2many field which is reffering to a single model, existing in different models.

ie,

    class modelA(models.Model):
       _name = 'modela'

       fila = fields.One2many('main','refa')

   class moddelB(models.Model):
      _name = 'modelb'

      filb = fields.One2many('main','refb')


   class main(models.Model):
     _name = 'main'

     name = fields.Char('Name')
     date = fields.Date('Date')
     refa = fields.Many2one('modela')
     refb = fields.Many2one('modelb')

I will create records in modela. In this model there is a button is there. On clicking on that button i need to copy all values of fila field to filb field of modelb . How can i do that.

2

2 Answers

1
votes

You need to use One2manu values filling

XML code

<button name="copy2b" type="object" string="COPY"/>  

Python code:

@api.multi
def copy2b(self):
    for record in self:
        filb_values = [(0, 0, {'name': line.name, 'date': line.date}) for line in record.fila]
        vals = {'filb': filb_values}
        # Just pass these values to `create` or `write` method to save them on `modelb`
0
votes
for record in ids:
    field_vals=[(0,0,{'product_name':line.product_id.name,'qty':line.quantity}) for line in record.invoice_line_ids]
    vals={'inv_products':field_vals}

    my_invoices=self.env["cron.invoice"].search([])
    my_invoices.write(vals)

worked for me. here, i am getting products from invoice_line_ids field of account.invoice model. "for record in ids" here ids is correspond invoice. then i am loop through invoice using "record" thus i can access invoice_line_ids one2many field of invoice. "inv_products" is my another one2many field in same model which is "cron.invoice". before wrote into same model i had use create method for create some fields. then i can use search() method. which will return all crated records.