Skip to content
Permalink
Browse files

[FIX] sale_stock: default warehouse_id

Finetuning of
b30e280
:

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.

X-original-commit: 6c6bd01
  • Loading branch information
Feyensv committed Mar 25, 2020
1 parent e4ebd3e commit b513bdc72fe800bbed3c5f4b2a78c70e447a7de2
Showing with 29 additions and 1 deletion.
  1. +2 −0 addons/sale_stock/models/res_users.py
  2. +27 −1 addons/sale_stock/models/sale_order.py
@@ -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/sale_order.py/_init_column.
return self.env['stock.warehouse'].search([('company_id', '=', self.env.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 odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT, float_compare
from odoo.exceptions import UserError

_logger = logging.getLogger(__name__)


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

@api.model
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', '=', self.env.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,))

@api.depends('picking_ids.date_done')
def _compute_effective_date(self):
for order in self:

0 comments on commit b513bdc

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