获取默认位置的方法
method to get default location
在 sale.order.line 中,我有字段 location_id,我希望它默认填满。问题是我用这段代码得到 TypeError: 'bool' object has no attribute '__getitem__'
而 self
总是空的。如果我将 if self.product_id:
添加到我的方法中,它就会停止工作。
class SaleOrderLine(models.Model):
_inherit = "sale.order.line"
def _get_default_location(self):
return self.env['stock.location'].search(['location_id', '=', self.product_id.warehouse_id.out_type_id.default_location_src_id.id], limit=1)
location_id = fields.Many2one('stock.location', 'Location', default=_get_default_location)
默认方法总是有空记录集。您不会在那里获得任何数据,除非您发现在调用默认方法之前将某些内容放入上下文中。
但在您的示例中,您可以使用 product_id 的 onchange 方法。我敢打赌,如果生产线上没有产品,您将不需要 location_id。因此,在 sale.order.line
上覆盖 product_id 的原始 onchange 方法,并始终设置您想要的默认值 location_id。 (我觉得应该叫product_id_change
)
def product_id_change(self):
res = super(SaleOrderLine, self).product_id_change()
if self.product_id:
self.location_id = # your search here
else:
self.location_id = False
return res
在 sale.order.line 中,我有字段 location_id,我希望它默认填满。问题是我用这段代码得到 TypeError: 'bool' object has no attribute '__getitem__'
而 self
总是空的。如果我将 if self.product_id:
添加到我的方法中,它就会停止工作。
class SaleOrderLine(models.Model):
_inherit = "sale.order.line"
def _get_default_location(self):
return self.env['stock.location'].search(['location_id', '=', self.product_id.warehouse_id.out_type_id.default_location_src_id.id], limit=1)
location_id = fields.Many2one('stock.location', 'Location', default=_get_default_location)
默认方法总是有空记录集。您不会在那里获得任何数据,除非您发现在调用默认方法之前将某些内容放入上下文中。
但在您的示例中,您可以使用 product_id 的 onchange 方法。我敢打赌,如果生产线上没有产品,您将不需要 location_id。因此,在 sale.order.line
上覆盖 product_id 的原始 onchange 方法,并始终设置您想要的默认值 location_id。 (我觉得应该叫product_id_change
)
def product_id_change(self):
res = super(SaleOrderLine, self).product_id_change()
if self.product_id:
self.location_id = # your search here
else:
self.location_id = False
return res