在sale.order.line我有字段location_id,我希望它会默认填满。问题是我得到TypeError: 'bool' object has no attribute '__getitem__'
with这个代码和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