如何在超级方法(创建)调用(odoo 12)中传递上下文

how to pass context inside super method (create) calling (odoo 12)

谁能告诉我如何在创建函数中使用 context 在两个模型(sale.order 和 stock.picking)之间传递数据?

class stock_backdoor(models.Model):

_inherit = 'stock.picking'

technician_id = fields.Many2one('hr.employee',string='Technician')
driver_id = fields.Many2one('hr.employee',string='Driver') 

@api.model
def create(self,vals):
   #here where i want to use the context to get the technichian   
   #and the driver id from (sale.order)
   return super(stock_backdoor, self).create(vals)

class sale_backdoor(models.Model):

_inherit = 'sale.order'

technician_id = fields.Many2one(
'hr.employee',string='Technician',required=True)

driver_id = fields.Many2one(
'hr.employee',string='Driver',required=True)

在超级调用之前放置 self = self.with_context(driver_id=self.driver_id.id)

你可以在超级调用中传递上下文,如下所示;

super(sale_backdoor, self.with_context(driver_id=vals.get('driver_id',False),technician_id=vals.get('technician_id',False)).create(vals)

请在销售订单模型中设置上下文,并在选股模型中接收如下:

driver_id = self._context.get('driver_id',False)
technician_id = self._context.get('technician_id',False)

请注意,根据您的问题,您确定要在从销售订单创建库存时将驱动程序和技术人员字段值从销售订单传递到库存。请使用下面的函数代码来做同样的事情:

在 Stock Move 模型中编写以下代码

class StockMove(models.Model):
    _inherit = "stock.move"

    def _get_new_picking_values(self):

        res = super(StockMove,self)._get_new_picking_values()
        res.update({'driver_id': self.sale_line_id and self.sale_line_id.order_id and self.sale_line_id.order_id.driver_id and self.sale_line_id.order_id.driver_id.id,
                    'technician_id': self.sale_line_id and self.sale_line_id.order_id and self.sale_line_id.order_id.technician_id and self.sale_line_id.order_id.technician_id.id,})
        return res