pythonodooodoo-12

Model not found in odoo 12


so I made a custom hospital module, but an error occurred when the module was upgraded, resulting in an error like this.

Error:
Odoo Server Error

Traceback (most recent call last):
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 1128, in _validate_fields
    check(self)
  File "c:\program files (x86)\odoo 12.0\server\odoo\addons\base\models\ir_ui_view.py", line 351, in _check_xml
    self.postprocess_and_fields(view.model, view_doc, view.id)
  File "c:\program files (x86)\odoo 12.0\server\odoo\addons\base\models\ir_ui_view.py", line 1105, in postprocess_and_fields
    self.raise_view_error(_('Model not found: %(model)s') % dict(model=model), view_id)
  File "c:\program files (x86)\odoo 12.0\server\odoo\addons\base\models\ir_ui_view.py", line 568, in raise_view_error
    raise ValueError(message)
ValueError: Model not found: rumahsakit.poli

Error context:
View `rumahsakit.poli.tree`
[view_id: 1519, xml_id: n/a, model: rumahsakit.poli, parent_id: n/a]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\tools\convert.py", line 758, in parse
    self._tags[rec.tag](rec, de, mode=mode)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\tools\convert.py", line 663, in _tag_record
    record = model.with_context(rec_context)._load_records([data], self.mode == 'update')
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 3910, in _load_records
    records = self._load_records_create([data['values'] for data in to_create])
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 3824, in _load_records_create
    return self.create(values)
  File "<decorator-gen-33>", line 2, in create
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\api.py", line 462, in _model_create_multi
    return create(self, arg)
  File "c:\program files (x86)\odoo 12.0\server\odoo\addons\base\models\ir_ui_view.py", line 428, in create
    return super(View, self).create(vals_list)
  File "<decorator-gen-3>", line 2, in create
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\api.py", line 462, in _model_create_multi
    return create(self, arg)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 3641, in create
    fields[0].determine_inverse(batch_recs)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\fields.py", line 1200, in determine_inverse
    getattr(records, self.inverse)()
  File "c:\program files (x86)\odoo 12.0\server\odoo\addons\base\models\ir_ui_view.py", line 275, in _inverse_arch
    view.write(data)
  File "c:\program files (x86)\odoo 12.0\server\odoo\addons\base\models\ir_ui_view.py", line 445, in write
    res = super(View, self).write(self._compute_defaults(vals))
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 3366, in write
    self._write(store_vals)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 3502, in _write
    self._validate_fields(vals)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 1132, in _validate_fields
    raise ValidationError("%s\n\n%s" % (_("Error while validating constraint"), tools.ustr(e)))
odoo.exceptions.ValidationError: ('Error while validating constraint\n\nModel not found: rumahsakit.poli\n\nError context:\nView `rumahsakit.poli.tree`\n[view_id: 1519, xml_id: n/a, model: rumahsakit.poli, parent_id: n/a]', None)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\http.py", line 656, in _handle_exception
    return super(JsonRequest, self)._handle_exception(exception)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\http.py", line 314, in _handle_exception
    raise pycompat.reraise(type(exception), exception, sys.exc_info()[2])
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\tools\pycompat.py", line 87, in reraise
    raise value
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\http.py", line 698, in dispatch
    result = self._call_function(**self.params)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\http.py", line 346, in _call_function
    return checked_call(self.db, *args, **kwargs)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\service\model.py", line 98, in wrapper
    return f(dbname, *args, **kwargs)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\http.py", line 339, in checked_call
    result = self.endpoint(*a, **kw)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\http.py", line 941, in __call__
    return self.method(*args, **kw)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\http.py", line 519, in response_wrap
    response = f(*args, **kw)
  File "c:\program files (x86)\odoo 12.0\server\odoo\addons\web\controllers\main.py", line 967, in call_button
    action = self._call_kw(model, method, args, {})
  File "c:\program files (x86)\odoo 12.0\server\odoo\addons\web\controllers\main.py", line 955, in _call_kw
    return call_kw(request.env[model], method, args, kwargs)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\api.py", line 759, in call_kw
    return _call_kw_multi(method, model, args, kwargs)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\api.py", line 746, in _call_kw_multi
    result = method(recs, *args, **kwargs)
  File "<decorator-gen-68>", line 2, in button_immediate_upgrade
  File "c:\program files (x86)\odoo 12.0\server\odoo\addons\base\models\ir_module.py", line 75, in check_and_log
    return method(self, *args, **kwargs)
  File "c:\program files (x86)\odoo 12.0\server\odoo\addons\base\models\ir_module.py", line 628, in button_immediate_upgrade
    return self._button_immediate_function(type(self).button_upgrade)
  File "c:\program files (x86)\odoo 12.0\server\odoo\addons\base\models\ir_module.py", line 562, in _button_immediate_function
    modules.registry.Registry.new(self._cr.dbname, update_module=True)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\modules\registry.py", line 86, in new
    odoo.modules.load_modules(registry._db, force_demo, status, update_module)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\modules\loading.py", line 417, in load_modules
    force, status, report, loaded_modules, update_module, models_to_check)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\modules\loading.py", line 313, in load_marked_modules
    perform_checks=perform_checks, models_to_check=models_to_check
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\modules\loading.py", line 222, in load_module_graph
    load_data(cr, idref, mode, kind='data', package=package, report=report)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\modules\loading.py", line 68, in load_data
    tools.convert_file(cr, package.name, filename, idref, mode, noupdate, kind, report)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\tools\convert.py", line 802, in convert_file
    convert_xml_import(cr, module, fp, idref, mode, noupdate, report)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\tools\convert.py", line 865, in convert_xml_import
    obj.parse(doc.getroot(), mode=mode)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\tools\convert.py", line 755, in parse
    self.parse(rec, mode)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\tools\convert.py", line 764, in parse
    exc_info[2]
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\tools\pycompat.py", line 86, in reraise
    raise value.with_traceback(tb)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\tools\convert.py", line 758, in parse
    self._tags[rec.tag](rec, de, mode=mode)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\tools\convert.py", line 663, in _tag_record
    record = model.with_context(rec_context)._load_records([data], self.mode == 'update')
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 3910, in _load_records
    records = self._load_records_create([data['values'] for data in to_create])
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 3824, in _load_records_create
    return self.create(values)
  File "<decorator-gen-33>", line 2, in create
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\api.py", line 462, in _model_create_multi
    return create(self, arg)
  File "c:\program files (x86)\odoo 12.0\server\odoo\addons\base\models\ir_ui_view.py", line 428, in create
    return super(View, self).create(vals_list)
  File "<decorator-gen-3>", line 2, in create
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\api.py", line 462, in _model_create_multi
    return create(self, arg)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 3641, in create
    fields[0].determine_inverse(batch_recs)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\fields.py", line 1200, in determine_inverse
    getattr(records, self.inverse)()
  File "c:\program files (x86)\odoo 12.0\server\odoo\addons\base\models\ir_ui_view.py", line 275, in _inverse_arch
    view.write(data)
  File "c:\program files (x86)\odoo 12.0\server\odoo\addons\base\models\ir_ui_view.py", line 445, in write
    res = super(View, self).write(self._compute_defaults(vals))
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 3366, in write
    self._write(store_vals)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 3502, in _write
    self._validate_fields(vals)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 1132, in _validate_fields
    raise ValidationError("%s\n\n%s" % (_("Error while validating constraint"), tools.ustr(e)))
odoo.tools.convert.ParseError: "Error while validating constraint

Model not found: rumahsakit.poli

Error context:
View `rumahsakit.poli.tree`
[view_id: 1519, xml_id: n/a, model: rumahsakit.poli, parent_id: n/a]
None" while parsing file:/e:/projects/addon_odoo/rumahsakit/views/poli.xml:4, near
<record model="ir.ui.view" id="rumahsakit_poli_tree">
            <field name="name">rumahsakit.poli.tree</field>
            <field name="model">rumahsakit.poli</field>
            <field name="arch" type="xml">
                <tree string="Poli" editabel="top">
                    <field name="kode"/>
                    <field name="name"/>
                </tree>
            </field>
        </record>

even though the model I created has initialized the model, here is the model for this module

from odoo import models, fields

class RumahSakitPoli(models.Model):
    _name = 'rumahsakit.poli'
    _description  = 'Poli'

    name = fields.Char(string='Nama Poli', required=True)
    kode = fields.Char(string='Kode Poli', required=True, copy=False)


    _sql_constraints = [
        ('unique_kode', 'unique(kode)', 'Kode Poli duplicated, mohon cek kembali !'),
    ]

    @api.multi
    def name_get(self):
        result = []
        for me in self:
            result.append((me.id, "%s-%s" %(me.kode, me.name)))
        return result

    @api.model
    def name_search(self, name, args=None, operator='ilike', limit=100):
        args = args or []
        if name:
            recs = self.seacrh([
                '|',
                ('kode', operator, name),
                ('name', operator, name)
            ] + args, limit=limit)
        else:
            recs = self.searc([] + args, limit=limit)
        return recs.name_get()

looks like an error occurred in the view file which can't capture the model

<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
    <data>
        <record model="ir.ui.view" id="rumahsakit_poli_tree">
            <field name="name">rumahsakit.poli.tree</field>
            <field name="model">rumahsakit.poli</field>
            <field name="arch" type="xml">
                <tree string="Poli" editabel="top">
                    <field name="kode"/>
                    <field name="name"/>
                </tree>
            </field>
        </record>

        <record id="view_ms_poli_search" model="ir.ui.view">
            <field name="name">rumahsakit.poli.search</field>
            <field name="model">rumahsakit.poli</field>
            <field name="arch" type="xml">
                <search string="Search Poli">
                    <field name="name"/>
                    <field name="kode"/>
                </search>
            </field>
        </record>

        <record model="ir.actions.act_window" id="rumahsakit_poli_action">
            <field name="name">Poli</field>
            <field name="res_model">rumahsakit.poli</field>
            <field name="view_type">form</field>
            <field name="view_mode">tree,form</field>
            <field name="context">{}</field>
            <field name="domain">[]</field>
        </record>

        <record id="rumahsakit_poli_action_tree" model="ir.actions.act_window.view">
            <field eval="1" name="sequence"/>
            <field name="view_mode">tree</field>
            <field name="view_id" ref="rumah_sakit_poli_view"/>
            <field name="act_window_id" ref="rumahsakit_poli_action"/>
        </record>
        <record id="rumahsakit_poli_action_tree" model="ir.actions.act_window.view">
            <field eval="1" name="sequence"/>
            <field name="view_mode">tree</field>
            <field name="view_id" ref="rumah_sakit_poli_view"/>
            <field name="act_window_id" ref="rumahsakit_poli_action"/>
        </record>

        <menuitem action="rumahsakit_poli_action" id="rumahsakit_poli_menu" parent="menu_rumahsakit_root" />
    </data>
</odoo>

I have also filled in the ir.model.access.csv file for security, and I have also done the import in the __init__.py file. I'm confused about what's going on, because I'm also still just learning odoo.


Solution

  • If you add a print('IMPORTED') in your model file, you can check whether or not it gets actually imported.

    I think it may be caused by the order of imports. Odoo loads its modules in a certain order, and if you have defined your view in an Odoo module that gets loaded before the one defining the model, it would cause this kind of error.
    Try to put both the view file and the model file in the same odoo module (same manifest).