简体   繁体   中英

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

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"

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM