odookeyerrorodoo-16

KeyError despite adding dependency


I've recently installed the Insurance Management module and I'm trying to enhance its functionality. My goal is to add a page in the CRM lead notebook to display insurances created for each lead.

However, I'm encountering an error every time I try to upgrade the CRM or the Insurance Management module. Below is the code I've added:

crm_lead.py in CRM Module:

from odoo import models, fields

class Lead(models.Model):
    _name = "crm.lead"
    _description = "Lead/Opportunity"
    _order = "priority desc, id desc"
    _inherit = ['mail.thread.cc', 'mail.thread.blacklist', 'mail.thread.phone', 'mail.activity.mixin', 'utm.mixin', 'format.address.mixin']

    insurance_ids = fields.One2many('insurance.details', 'crm_lead_ref', string='Insurances', required=True, store=True)

    def create_insurance_action(self):
        return {
            'name': 'Create Insurance',
            'type': 'ir.actions.act_window',
            'res_model': 'insurance.details',
            'view_mode': 'form',
            'target': 'new',
            'context': {
                'default_partner_id': self.partner_id.id,
                'default_lead_id': self.id
            },
            'views': [(False, 'form')],
            'view_id': False,
        }

insurance_details.py in Insurance Management module:

from odoo import models, fields

class InsuranceDetails(models.Model):
    _name = 'insurance.details'
    _inherit = ["mail.thread"]

    crm_lead_ref = fields.Many2one('crm.lead', string='CRM Lead Reference', help="Reference to the CRM lead from which this insurance was created.", required=True, store=True)

    def create(self, vals):
        if vals.get('name', 'New') == 'New':
            vals['name'] = self.env['ir.sequence'].next_by_code('insurance.details') or 'New'
        
        crm_lead_ref = self.env.context.get('default_crm_lead_ref')
        if crm_lead_ref:
            vals['crm_lead_ref'] = crm_lead_ref
        
        new_record = super(InsuranceDetails, self).create(vals)
        return new_record

manifest.py in insurance module:

{
    'name': 'Insurance Management',
    'version': '16.0.1.1.1',
    'summary': """Insurance Management & Operations""",
    'description': """Insurance Management""",
    'author': 'Cybrosys Techno Solutions',
    'company': 'Cybrosys Techno Solutions',
    'category': 'Industries',
    'depends': ['crm', 'account', 'base'],
    'license': 'AGPL-3',
    'data': [
        'security/ir.model.access.csv',
        'data/insurance_management_data.xml',
        'views/claim_details_views.xml',
        'views/employee_details_views.xml',
        'views/insurance_details_views.xml',
        'views/policy_details_views.xml',
        'views/insurance_management_menus.xml'
    ],
}

crm_lead_views.xml:

                <page string="Insurances">
                    <field name="insurance_ids">
                         <tree>
                        </tree>
                    </field>
                </page> 

I have extended the crm.lead model with a One2many field to insurance.details, and also have a corresponding Many2one field in insurance.details. Despite these configurations, I keep getting an error during the module upgrade.


RPC_ERROR
Odoo Server Error
Traceback (most recent call last):
  File "/Users/kamil/work/odoo/odoo/service/model.py", line 133, in retrying
    result = func()
             ^^^^^^
  File "/Users/kamil/work/odoo/odoo/http.py", line 1618, in _serve_ir_http
    response = self.dispatcher.dispatch(rule.endpoint, args)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kamil/work/odoo/odoo/http.py", line 1822, in dispatch
    result = self.request.registry['ir.http']._dispatch(endpoint)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kamil/work/odoo/odoo/addons/base/models/ir_http.py", line 154, in _dispatch
    result = endpoint(**request.params)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kamil/work/odoo/odoo/http.py", line 697, in route_wrapper
    result = endpoint(self, *args, **params_ok)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kamil/work/odoo/addons/web/controllers/dataset.py", line 46, in call_button
    action = self._call_kw(model, method, args, kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kamil/work/odoo/addons/web/controllers/dataset.py", line 33, in _call_kw
    return call_kw(request.env[model], method, args, kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kamil/work/odoo/odoo/api.py", line 466, in call_kw
    result = _call_kw_multi(method, model, args, kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kamil/work/odoo/odoo/api.py", line 453, in _call_kw_multi
    result = method(recs, *args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<decorator-gen-83>", line 2, in button_immediate_upgrade
  File "/Users/kamil/work/odoo/odoo/addons/base/models/ir_module.py", line 74, in check_and_log
    return method(self, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kamil/work/odoo/odoo/addons/base/models/ir_module.py", line 659, in button_immediate_upgrade
    return self._button_immediate_function(type(self).button_upgrade)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kamil/work/odoo/odoo/addons/base/models/ir_module.py", line 592, in _button_immediate_function
    registry = modules.registry.Registry.new(self._cr.dbname, update_module=True)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<decorator-gen-16>", line 2, in new
  File "/Users/kamil/work/odoo/odoo/tools/func.py", line 87, in locked
    return func(inst, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kamil/work/odoo/odoo/modules/registry.py", line 90, in new
    odoo.modules.load_modules(registry, force_demo, status, update_module)
  File "/Users/kamil/work/odoo/odoo/modules/loading.py", line 484, in load_modules
    processed_modules += load_marked_modules(cr, graph,
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kamil/work/odoo/odoo/modules/loading.py", line 372, in load_marked_modules
    loaded, processed = load_module_graph(
                        ^^^^^^^^^^^^^^^^^^
  File "/Users/kamil/work/odoo/odoo/modules/loading.py", line 207, in load_module_graph
    registry.setup_models(cr)
  File "<decorator-gen-19>", line 2, in setup_models
  File "/Users/kamil/work/odoo/odoo/tools/func.py", line 87, in locked
    return func(inst, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kamil/work/odoo/odoo/modules/registry.py", line 298, in setup_models
    model._setup_fields()
  File "/Users/kamil/work/odoo/odoo/models.py", line 2813, in _setup_fields
    field.setup(self)
  File "/Users/kamil/work/odoo/odoo/fields.py", line 538, in setup
    self.setup_nonrelated(model)
  File "/Users/kamil/work/odoo/odoo/fields.py", line 4332, in setup_nonrelated
    invf = comodel._fields[self.inverse_name]
           ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
KeyError: 'crm_lead_ref'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/kamil/work/odoo/odoo/modules/registry.py", line 64, in __new__
    return cls.registries[db_name]
           ~~~~~~~~~~~~~~^^^^^^^^^
  File "<decorator-gen-8>", line 2, in __getitem__
  File "/Users/kamil/work/odoo/odoo/tools/func.py", line 87, in locked
    return func(inst, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kamil/work/odoo/odoo/tools/lru.py", line 34, in __getitem__
    a = self.d[obj]
        ~~~~~~^^^^^
KeyError: 'odoo'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/kamil/work/odoo/odoo/http.py", line 1591, in _serve_db
    return service_model.retrying(self._serve_ir_http, self.env)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kamil/work/odoo/odoo/service/model.py", line 167, in retrying
    env.reset()
  File "/Users/kamil/work/odoo/odoo/api.py", line 501, in reset
    self.transaction.reset()
  File "/Users/kamil/work/odoo/odoo/api.py", line 887, in reset
    self.registry = Registry(self.registry.db_name)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kamil/work/odoo/odoo/modules/registry.py", line 66, in __new__
    return cls.new(db_name)
           ^^^^^^^^^^^^^^^^
  File "<decorator-gen-16>", line 2, in new
  File "/Users/kamil/work/odoo/odoo/tools/func.py", line 87, in locked
    return func(inst, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kamil/work/odoo/odoo/modules/registry.py", line 90, in new
    odoo.modules.load_modules(registry, force_demo, status, update_module)
  File "/Users/kamil/work/odoo/odoo/modules/loading.py", line 510, in load_modules
    registry.setup_models(cr)
  File "<decorator-gen-19>", line 2, in setup_models
  File "/Users/kamil/work/odoo/odoo/tools/func.py", line 87, in locked
    return func(inst, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kamil/work/odoo/odoo/modules/registry.py", line 298, in setup_models
    model._setup_fields()
  File "/Users/kamil/work/odoo/odoo/models.py", line 2813, in _setup_fields
    field.setup(self)
  File "/Users/kamil/work/odoo/odoo/fields.py", line 538, in setup
    self.setup_nonrelated(model)
  File "/Users/kamil/work/odoo/odoo/fields.py", line 4332, in setup_nonrelated
    invf = comodel._fields[self.inverse_name]
           ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
KeyError: 'crm_lead_ref'

The above server error caused the following client error:
RPC_ERROR: Odoo Server Error
    RPCError@http://localhost:8069/web/assets/963-81d456c/web.assets_backend.min.js:987:274
    makeErrorFromResponse@http://localhost:8069/web/assets/963-81d456c/web.assets_backend.min.js:991:163
    jsonrpc/promise</<@http://localhost:8069/web/assets/963-81d456c/web.assets_backend.min.js:999:34

Any insights or suggestions on what might be causing this issue or how to resolve it would be greatly appreciated


Solution

  • This is the proper way to inherit a model; your current approach of inheriting modules is incorrect.

    class Lead(models.Model):
        _inherit = 'crm.lead'
    
        insurance_ids = fields.One2many('insurance.details', 'crm_lead_id', string='Insurances', required=True)
    
        def create_insurance_action(self):
            return {
                'name': 'Create Insurance',
                'type': 'ir.actions.act_window',
                'res_model': 'insurance.details',
                'view_mode': 'form',
                'target': 'new',
                'context': {
                    'default_partner_id': self.partner_id.id,
                    'default_crm_lead_id': self.id
                },
                'views': [(False, 'form')],
                'view_id': False,
            }
    

    Next, you need to create a may2one field in the name of the reverse field, which is "crm_lead_id," in the comodel, which is "insurance.details," in order to use a One2many field insurance_ids inside the model "crm.lead" with reverse field "crm_lead_id."

    class InsuranceDetails(models.Model):
        _name = 'insurance.details'
    
        crm_lead_id = fields.Many2one('crm.lead') 
    
        @api.model
        def create(self, vals):
            if vals.get('name', 'New') == 'New':
                vals['name'] = self.env['ir.sequence'].next_by_code(
                    'insurance.details') or 'New'
            crm_lead_id = self.env.context.get('default_crm_lead_id')
            if crm_lead_id:
                vals['crm_lead_id'] = crm_lead_id
            return super(InsuranceDetails, self).create(vals)
    

    Lastly, override the create method as it is given.