KeyError: 'stock_move_id'

38 Views Asked by At

I'm trying to update a custom module in Odoo14 whereby I want to introduce a new field or column in the account.move module that tells me which stock.move movement ID, if any, each line in account.move corresponds to. Below I indicate the code with which I intend to create the new column. But I get the following error:

Error:
Odoo Server Error

Traceback (most recent call last):
  File "/opt/odoo14/odoo14/odoo/addons/base/models/ir_http.py", line 237, in _dispatch
    result = request.dispatch()
  File "/opt/odoo14/odoo14/odoo/http.py", line 685, in dispatch
    result = self._call_function(**self.params)
  File "/opt/odoo14/odoo14/odoo/http.py", line 361, in _call_function
    return checked_call(self.db, *args, **kwargs)
  File "/opt/odoo14/odoo14/odoo/service/model.py", line 94, in wrapper
    return f(dbname, *args, **kwargs)
  File "/opt/odoo14/odoo14/odoo/http.py", line 349, in checked_call
    result = self.endpoint(*a, **kw)
  File "/opt/odoo14/odoo14/odoo/http.py", line 908, in __call__
    return self.method(*args, **kw)
  File "/opt/odoo14/odoo14/odoo/http.py", line 533, in response_wrap
    response = f(*args, **kw)
  File "/opt/odoo14/odoo14/addons/web/controllers/main.py", line 1376, in call_button
    action = self._call_kw(model, method, args, kwargs)
  File "/opt/odoo14/odoo14/addons/web/controllers/main.py", line 1364, in _call_kw
    return call_kw(request.env[model], method, args, kwargs)
  File "/opt/odoo14/odoo14/odoo/api.py", line 399, in call_kw
    result = _call_kw_multi(method, model, args, kwargs)
  File "/opt/odoo14/odoo14/odoo/api.py", line 386, in _call_kw_multi
    result = method(recs, *args, **kwargs)
  File "<decorator-gen-77>", line 2, in button_immediate_upgrade
  File "/opt/odoo14/odoo14/odoo/addons/base/models/ir_module.py", line 74, in check_and_log
    return method(self, *args, **kwargs)
  File "/opt/odoo14/odoo14/odoo/addons/base/models/ir_module.py", line 658, in button_immediate_upgrade
    return self._button_immediate_function(type(self).button_upgrade)
  File "/opt/odoo14/odoo14/odoo/addons/base/models/ir_module.py", line 596, in _button_immediate_function
    modules.registry.Registry.new(self._cr.dbname, update_module=True)
  File "/opt/odoo14/odoo14/odoo/modules/registry.py", line 89, in new
    odoo.modules.load_modules(registry._db, force_demo, status, update_module)
  File "/opt/odoo14/odoo14/odoo/modules/loading.py", line 455, in load_modules
    processed_modules += load_marked_modules(cr, graph,
  File "/opt/odoo14/odoo14/odoo/modules/loading.py", line 347, in load_marked_modules
    loaded, processed = load_module_graph(
  File "/opt/odoo14/odoo14/odoo/modules/loading.py", line 173, in load_module_graph
    registry.setup_models(cr)
  File "/opt/odoo14/odoo14/odoo/modules/registry.py", line 276, in setup_models
    model._setup_fields()
  File "/opt/odoo14/odoo14/odoo/models.py", line 2848, in _setup_fields
    field.setup_full(self)
  File "/opt/odoo14/odoo14/odoo/fields.py", line 418, in setup_full
    self._setup_regular_full(model)
  File "/opt/odoo14/odoo14/odoo/fields.py", line 3130, in _setup_regular_full
    invf = comodel._fields[self.inverse_name]
Exception

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/opt/odoo14/odoo14/odoo/http.py", line 641, in _handle_exception
    return super(JsonRequest, self)._handle_exception(exception)
  File "/opt/odoo14/odoo14/odoo/http.py", line 317, in _handle_exception
    raise exception.with_traceback(None) from new_cause
KeyError: 'stock_move_id'

I think that de problem gos for the new colom for account.move:

class AccountMove(models.Model):
    _inherit = "account.move"

    stock_move_id = fields.Many2one(
        'stock.move',
        string='Stock Move',
        compute='_compute_stock_move_id',
        store=True,
        readonly=True
    )

    def _compute_stock_move_id(self):
        for move in self:
            stock_moves = move.line_ids.mapped('stock_move_id')
            move.stock_move_id = stock_moves[0] if stock_moves else False
0

There are 0 best solutions below