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

时间:2019-08-21 08:39:20

标签: odoo-12

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

stock_backdoor(models.Model)类:

[<nil> <nil> <nil> <nil>]

sale_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)

2 个答案:

答案 0 :(得分:0)

在超级调用之前输入self = self.with_context(driver_id = self.driver_id.id)

答案 1 :(得分:0)

您可以在超级调用中通过以下方式传递上下文;

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