空对象中的self为空,所以不能通过self.field=xxx进行赋值,所以可以通过原生方法中的default_get()进行赋值,代码如下:
@api.model
def default_get(self, fields):
res = super(AddProductionItem, self).default_get(fields)
if self.env.context['active_model'] == 'stock.move':
move = self.env['stock.move'].browse(self.env.context['active_id'])
res.update({'product_id': move.product_id.id,
'production_id':move.raw_material_production_id.id})
res = self._convert_to_write(self._convert_to_cache(res))
return res
注意:最后需要调用模型的_convert_to_write()方法。