can anybody tell me how I can use context to pass data between two models (sale.order and stock.picking) in the create function?
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)
You can pass context as below in super call;
super(sale_backdoor, self.with_context(driver_id=vals.get('driver_id',False),technician_id=vals.get('technician_id',False)).create(vals)
Please set the context in sale order model and receive it in stock picking model as below:
driver_id = self._context.get('driver_id',False)
technician_id = self._context.get('technician_id',False)
Please be informed that from your question it is sure that you want to pass driver and technician field values from sale order to stock while creating stock from sale order. Please use the below function code to do the same:
Write the below code in Stock Move model
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