Return Many2One 字段列表

Return a list to Many2One field

我有一个 class Branch、class BranchLine、class Product 和 class ReqTrans,我的想法是从一个分支发出产品的转移请求另一方面,我想做的是当用户选择某个分支时,可用产品应该只可见 classes 如下

class CustomBranch(models.Model): 
    _name = 'custom.branch' 
    _description = 'Branch Record' 
    _rec_name = 'branch_name' 

    branch_name = fields.Char(string="Branch Name", required=False, ) 
    branch_line = fields.One2many('custom.branch.line', 'branch_id', string='Branch Lines', )

class CustomBranchLine(models.Model): 
    _name = 'custom.branch.line' 
    _description = 'Branch Line Record' 

    branch_id = fields.Many2one('custom.branch', string='Branch') 
    product_id = fields.Many2one('custom.product', string='Product') 
    qty = fields.Integer(string="QTY", required=False, )

class CustomTransRequest(models.Model): 
    _name = 'custom.trans.request' 
    _description = 'Transfer Request' 

branch_from_id = fields.Many2one('custom.branch', string="From") 
branch_to_id = fields.Many2one('custom.branch', string="To") 
product_ids = fields.Many2many('custom.product', string="Line") 

@api.onchange('branch_from_id') 
def onchange_branch_from(self): 
        for rec in self: 
            selected_lines = rec.env['custom.branch.line'].search([('branch_id', '=', rec.branch_from_id.id)]) 
            products = [] 
            for line in selected_lines: 
                 products.append([line.product_id.product_name, line.id]) 
        return products 

我如何 return 产品到 products_ids?

@api.onchange('branch_from_id') 
def onchange_branch_from(self):
   for rec in self: 
      selected_products = rec.env['custom.branch.line'].search([('branch_id', '=', rec.branch_from_id.id)]).mapped('product_id')
      self.product_ids = [(6, 0, selected_products.ids)]

试试这个