Skip to content
Browse files

[FIX] sale_stock: default warehouse_id

Finetuning of

self.user_id is always an empty recordset in a default function call,
use self.env.user instead, it is the default value for user_id.

The _init_column override is needed to ensure the new property field on
res_users isn't read at module installation, because its ir.model.fields
doesn't exist already, leading to a wrong sql query in ir_property.

closes #48378

X-original-commit: 6c6bd01
Signed-off-by: Victor Feyens (vfe) <>
  • Loading branch information
Feyensv committed Mar 25, 2020
1 parent 5eea6b2 commit 466217ff6cc6c30a4014b4fb0cc0566f32869268
Showing with 29 additions and 1 deletion.
  1. +2 −0 addons/sale_stock/models/
  2. +27 −1 addons/sale_stock/models/
@@ -12,6 +12,8 @@ class Users(models.Model):
def _get_default_warehouse_id(self):
if self.property_warehouse_id:
return self.property_warehouse_id
# !!! Any change to the following search domain should probably
# be also applied in sale_stock/models/
return self.env['stock.warehouse'].search([('company_id', '=',], limit=1)

def __init__(self, pool, cr):
@@ -2,20 +2,25 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.

import json
import logging
from datetime import datetime, timedelta
from collections import defaultdict

from odoo import api, fields, models, _
from import DEFAULT_SERVER_DATETIME_FORMAT, float_compare
from odoo.exceptions import UserError

_logger = logging.getLogger(__name__)

class SaleOrder(models.Model):
_inherit = "sale.order"

def _default_warehouse_id(self):
return self.user_id._get_default_warehouse_id()
# !!! Any change to the default value may have to be repercuted
# on _init_column() below.
return self.env.user._get_default_warehouse_id()

incoterm = fields.Many2one(
'account.incoterms', 'Incoterm',
@@ -42,6 +47,27 @@ def _default_warehouse_id(self):
json_popover = fields.Char('JSON data for the popover widget', compute='_compute_json_popover')
show_json_popover = fields.Boolean('Has late picking', compute='_compute_json_popover')

def _init_column(self, column_name):
""" Ensure the default warehouse_id is correctly assigned
At column initialization, the ir.model.fields for res.users.property_warehouse_id isn't created,
which means trying to read the property field to get the default value will crash.
We therefore enforce the default here, without going through
the default function on the warehouse_id field.
if column_name != "warehouse_id":
return super(SaleOrder, self)._init_column(column_name)
field = self._fields[column_name]
default = self.env['stock.warehouse'].search([('company_id', '=',], limit=1)
value = field.convert_to_write(default, self)
value = field.convert_to_column(value, self)
if value is not None:
_logger.debug("Table '%s': setting default value of new column %s to %r",
self._table, column_name, value)
query = 'UPDATE "%s" SET "%s"=%s WHERE "%s" IS NULL' % (
self._table, column_name, field.column_format, column_name)
self._cr.execute(query, (value,))

def _compute_effective_date(self):
for order in self:

0 comments on commit 466217f

Please sign in to comment.
You can’t perform that action at this time.