Search code examples
odooodoo-8odoo-9

method to get default location


In sale.order.line I have field location_id and I want that it would fill up by default. the problem is that i get TypeError: 'bool' object has no attribute '__getitem__'with this code and self always comes empty. if I add if self.product_id: to my method it just stops working.

    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)

Solution

  • Default methods always have empty recordsets. You won't get any data there, except you find out to get something into the context before the default method is called.

    But in your example you just could use the onchange method for product_id. I bet you won't need a location_id without a product on a line. So override the original onchange method for product_id on sale.order.line and always set your desired default location_id. (I think it's called 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