You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

创建XML文件后出现Odoo Server Error的原因及解决求助

问题描述

添加views/xml文件并以超级用户访问自定义模块后,触发如下错误:

RPC_ERROR Odoo Server Error

已尝试的操作:

  • 创建新数据库及账号
  • 修改odoo.conf配置内容:
[options]
; This is the password that allows database operations:
; admin_passwd = admin
db_host = False
db_port = False
db_user = odoo16v1
db_password = odoo16v1
addons_path = server/addons , server/customaddons , C:/Odoo/odoo16/server/customaddons
default_productivity_apps = True
http_port = 8050

但在新数据库中安装该应用并刷新后,出现cancel install提示,最终报错信息为KeyError: 'hospital.patient'。更换数据库账号重新安装,搜索应用时仍显示cancel install。

模块代码如下:

oom_hospital/manifest.py

{
    'name': 'Hospital Management V1',
    'author': 'odoodev16v1',
    'data': [
        'views/menu.xml',
        'views/patient_view.xml'
    ]
}

oom_hospital/views/menu.xml

<?xml version="1.0" encoding="utf-8"?>
<odoo>

    <menuitem
            id="menu_hospital_root"
            name="Hospital"
            sequence="0"
    />

    <menuitem
            id="menu_patient_master"
            name="Patient Details"
            parent="menu_hospital_root"
            sequence="0"
    />
</odoo>

oom_hospital/views/patient_view.xml

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <record id="action_hospital_patient" model="ir.actions.act_window">
        <field name="name">Patients</field>
        <field name="type">ir.actions.act_window</field>
        <field name="res_model">hospital.patient</field>
        <field name="view_mode">tree,form</field>
        <field name="context">{}</field>
        <field name="help" type="html">
            <p class="no_view_nocontent_smilling_face">
                Create your first patient
            </p>
        </field>
    </record>

    <menuitem
            id="menu_patient"
            name="Patient"
            action="action_hospital_patient"
            parent="menu_patient_master"
            sequence="0"
    />

</odoo>

解决方案

核心原因

报错KeyError: 'hospital.patient'的本质是:你在patient_view.xml的动作定义里引用了hospital.patient模型,但模块中完全没有定义这个模型,Odoo找不到对应模型,导致安装流程中断。

修复步骤

  1. 创建模型文件
    在oom_hospital模块目录下新建models文件夹,然后创建patient.py文件,写入以下代码定义模型:
from odoo import models, fields

class HospitalPatient(models.Model):
    _name = 'hospital.patient'
    _description = '医院患者'

    name = fields.Char(string='姓名', required=True)
    # 可根据需求添加更多字段,比如age = fields.Integer(string='年龄')等
  1. 更新模块清单文件
    修改__manifest__.py,添加对base模块的依赖(Odoo基础模块,确保核心功能正常),Odoo会自动加载models目录下的所有文件:
{
    'name': 'Hospital Management V1',
    'author': 'odoodev16v1',
    'depends': ['base'],
    'data': [
        'views/menu.xml',
        'views/patient_view.xml'
    ]
}
  1. 补充视图定义(可选但必要)
    当前patient_view.xml只定义了动作,没有对应的tree和form视图,即使模型创建成功,进入菜单后仍会报错。需在patient_view.xml中补充视图代码:
<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <!-- 动作定义 -->
    <record id="action_hospital_patient" model="ir.actions.act_window">
        <field name="name">Patients</field>
        <field name="type">ir.actions.act_window</field>
        <field name="res_model">hospital.patient</field>
        <field name="view_mode">tree,form</field>
        <field name="context">{}</field>
        <field name="help" type="html">
            <p class="no_view_nocontent_smilling_face">
                Create your first patient
            </p>
        </field>
    </record>

    <!-- 列表视图 -->
    <record id="view_hospital_patient_tree" model="ir.ui.view">
        <field name="name">hospital.patient.tree</field>
        <field name="model">hospital.patient</field>
        <field name="arch" type="xml">
            <tree>
                <field name="name"/>
                <!-- 添加其他需要显示的字段 -->
            </tree>
        </field>
    </record>

    <!-- 表单视图 -->
    <record id="view_hospital_patient_form" model="ir.ui.view">
        <field name="name">hospital.patient.form</field>
        <field name="model">hospital.patient</field>
        <field name="arch" type="xml">
            <form>
                <sheet>
                    <group>
                        <field name="name"/>
                        <!-- 添加其他需要编辑的字段 -->
                    </group>
                </sheet>
            </form>
        </field>
    </record>

    <!-- 菜单项 -->
    <menuitem
            id="menu_patient"
            name="Patient"
            action="action_hospital_patient"
            parent="menu_patient_master"
            sequence="0"
    />

</odoo>
  1. 重新安装模块
  • 重启Odoo服务
  • 进入目标数据库,找到该模块重新安装

完成以上操作后,模块即可正常安装,hospital.patient模型会被正确识别,KeyError问题不再出现。


内容的提问来源于stack exchange,提问作者Stykgwar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 01:53:15