On a button click show the products in a one2many field in another one2many field in a different model

445 Views Asked by At

I am trying to access a one2many field from a specific model and display it in another one, both models already exist and I am applying few changes to them. I inherited the first model and added a button to it and upon pressing it, this button should manipulate my one2many field from another model and here is the code, any help concerning this :

from odoo import models,api,fields
    
class Approval(models.Model):
     _inherit = "approval.request"
    
    line_ids = fields.One2many('purchase.requisition.line', 'requisition_id', string='Products to Purchase', states={'done': [('readonly', True)]}, copy=True)
    
    product_line_ids = fields.One2many(related='line_ids.product_id', string='Type')

    
    def purchase_agreement(self):
        for rec in self:
            lines = []
            for line in rec.product_line_ids:
                vals = {
                    'line_ids': line.line_ids,
                }

                lines.append((0, 0, vals))
            rec.product_line_ids = lines
        
    
class PurchaseReq(models.Model):
    _inherit="purchase.requisition"
1

There are 1 best solutions below

2
altela On

Maybe try this one :

class Approval(models.Model): 

    _inherit = "approval.request"


    line_ids = fields.One2many('purchase.requisition.line', 'requisition_id', string='Products to Purchase', states={'done': [('readonly', True)]}, copy=True)
    product_line_ids = fields.One2many(related='line_ids.product_id', string='Type')


    def purchase_agreement(self):
        for rec in self:
            self.product_line_ids = [(0, 0,
                                   {'product_line_ids': rec.line_ids})]
            
            self.product_lines_ids  = self.line_ids

upon pressing purchase_agreement button, the self.product_line_ids will assign itself through backend with rec.line_ids based on user's selection in line_ids. Later on, to make the previous changes also work in the front end, self.product_lines_ids = self.line_ids will do.

Please let me know the update