在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)
答案 0 :(得分:1)
默认方法始终具有空记录集。您不会在那里获得任何数据,除非您可以在调用默认方法之前先将某些内容放入上下文中。
但是在您的示例中,您只可以对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