惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Security @ Cisco Blogs
The Last Watchdog
The Last Watchdog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
aimingoo的专栏
aimingoo的专栏
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
PCI Perspectives
PCI Perspectives
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
月光博客
月光博客
V
Visual Studio Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Tailwind CSS Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
L
LangChain Blog
B
Blog RSS Feed
小众软件
小众软件
N
News | PayPal Newsroom
Attack and Defense Labs
Attack and Defense Labs
Microsoft Azure Blog
Microsoft Azure Blog
V
Vulnerabilities – Threatpost
The Hacker News
The Hacker News
T
Tor Project blog
A
Arctic Wolf
Jina AI
Jina AI
Hacker News: Ask HN
Hacker News: Ask HN
F
Fortinet All Blogs
Cloudbric
Cloudbric
S
Secure Thoughts
L
LINUX DO - 热门话题
博客园 - 司徒正美
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
S
Security Affairs
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
J
Java Code Geeks
P
Privacy International News Feed
AWS News Blog
AWS News Blog
S
Securelist
TaoSecurity Blog
TaoSecurity Blog
AI
AI
O
OpenAI News
C
Cyber Attacks, Cyber Crime and Cyber Security
K
Kaspersky official blog
T
The Blog of Author Tim Ferriss
大猫的无限游戏
大猫的无限游戏
Google DeepMind News
Google DeepMind News
Know Your Adversary
Know Your Adversary
P
Palo Alto Networks Blog
T
Tenable Blog
Last Week in AI
Last Week in AI
WordPress大学
WordPress大学
S
SegmentFault 最新的问题

博客园_首页

Plist 二进制格式 Milvus 和 PGVector,哪个更好? OpenClaw 已过时?在 VS Code 中运行 Hermes Agent! 分享一下笔者的 Mac 装机必备软件 第30篇文章:一个大三计科生的自白 Manim如何在数学公式中完美显示中文? Docker 部署 RocketMQ 5 并发编程核心概念辨析 C#事务处理最佳实践:别再让“主表存了、明细丢了”的破事发生 CLI 是什么?为什么大厂突然集体卷命令行? 【从0到1构建一个ClaudeAgent】协作-自主Agent UIImageView 设置图片不生效的原因排查 最小二乘问题详解20:无先验约束下的增量式SFM自由网平差 痞子衡嵌入式:大话双核i.MXRT1180之XIP应用里借助MU实现可靠Flash IAP的方法 AI Chat 封装, SemanticKerne.AiProvider.Unified 已发布 Windows下右键编辑js文件无法打开记事本——在注册表中使用环境变量 在后台服务中使用 Scoped 服务,为什么总是报错? H200 安装驱动并使用sglang启动模型 wireshark 抓包Trap上报告警内容 我用 AI 辅助开发了一系列小工具(2):图片压缩工具 [A Primer On MC and CC] 2.1 Memory Consistency 1 - 指令重排序和 SC 模型 Oracle数据库SCN推进技术详解与实践指南 玩转控件:封装个带图片的Label控件 Claude Code 4.7 真正该升级的不是模型,而是你的工作流 前端小白一句话,AI 帮我做了个颜值拉满的桌面媒体播放器。当代码不再是门槛,一句话编程就是现实。 5. WorkBuddy: 小龙虾的灵魂三件套,让你的小龙虾不只是工具 SQLite 分片方案实战:三种分片策略的深度对比 告别简陋 UI!一款基于 Fluent Design 和基于 WinUI 的开源免费、现代化的 Avalonia UI 控件库 关于二进制排列组合枚举的总结 AI开发-python-LangGraph框架(3-27-LangGraph从零实现大模型智能决策工作流) ElasticSearch主分片和副本分片概念详解 【002】HTTPS 粗解:证书、TLS 握手与对后端配置的影响 Hermes Agent 一周暴涨五万 Star,但我劝你别急着追 明明连接的是Redis的DB0,为什么能查到DB3的数据? 【从0到1构建一个ClaudeAgent】协作-Agent团队 熟悉电子元器件之后,电子小白下一步该怎么走? MAF快速入门(23)通过C#类定义Skills .NET 高级开发 | 手写一个对象映射框架 FastAPI数据库ORM怎么选?我肝了三个Demo后,终于不再纠结了 mysqldump 参数拾遗:在遗忘与铭记之间 C# .NET 周刊|2026年3月5期 Claude code入门 - 陈彦斌 一文学习入门 ThingsBoard 开源物联网平台 GitHub 热门项目 | 2026年04月16日 如何为GIT设置全局勾子,为每次提交追加信息 Number.isFinite和isFinite与isNaN()和Number.isNaN的区别 PortSwigger SQL注入LAB2 推荐一个测试人必备的Skills,从功能到性能全搞定(附详细实操和安装下载方式) GLM模型这么火,咱们用vllm也咧一个呗! 深入理解 AbortController:从底层原理到跨语言设计哲学 字符串学习笔记 多租户系统框架的基础模块设计和分析设计 Apache SeaTunnel Zeta 为什么能做到“又快又稳”? AI开发-python-LangGraph框架(3-26-LangGraph基本概念及第一个简单样例) Vue 3 组件通信,别只会用 Props 和 Emits 了,这几个狠活儿你得看看 ElasticSearch7.X版本配置密码 用Manim实现动态交点计算--从一个动点问题说起 团结引擎+Addressable+Instant Game打包抖音小游戏 function call 实战:让 LLM 自动判断 pod 异常、调用日志工具并完成故障分析 bubseek —— 让 Agent 的足迹,变成团队的洞察 通过 C# 读取并导出 PDF 书签 如何用 GitHub Actions 实现 Steam 自动化发布 【从0到1构建一个ClaudeAgent】并发-后台任务 .NET 高级开发 | 定制 ASP.NET Core 框架 电子小白:什么是运算放大器(运放) zero2Agent:面向大厂面试的 Agent 工程教程,从概念到生产的完整学习路线 堆上的ORW HC32F460 USB CDC通信异常:非对齐访问异常排查 20260413-Hyperbridge 攻击事件:发生在默克尔山上的验证绕过 那些喊着AI 要淘汰你的人,正在靠你的焦虑赚大钱! 深度学习进阶(八)Swin Transformer 最小二乘问题详解19:带先验约束的增量式SFM优化与实现 SnapTranslate 3.0 正式发布:全局划词翻译 + 完整英语学习闭环,一站式搞定查词、记词、复习 工作的意义、工作的困难认知再思考 .NET + AI 进阶实战:基于类的技能开发 - 打造可治理的 Agent 能力模块 【从0到1构建一个ClaudeAgent】规划与协调-技能 上周热点回顾(4.6-4.12) 电子小白的工具三件套:面包板、杜邦线、万能板 单表五亿数据的查询优化 | Mysql、StarRocks 2. WorkBuddy:从“我是谁”到“帮我干活” C# 如何减少代码运行时间:7 个实战技巧 基于HelixToolkit.SharpDX 渲染3D模型 - 笺上知微 从零开始的双臂具身VLA起源及现阶段发展综述 - SkyXZ 记对 xonsh shell 的使用, 脚本编写, 迁移及调优 - pluvium27 受够了Vibe Coding的失控?换个起点,让AI事半功倍 从开始配置漏洞环境到漏洞复现流程 - 難しい 关于10年工作经验的程序员对OpenClaw的实战经验分享以及看法 - 虚无境 Any metadata 的内存布局 C# .NET 周刊|2026年3月2期 - InCerry 我帮你测过了,测试圈排名第二的 Skill 依然很牛逼 Skill Discovery | 无监督技能发现的经典工作总结 - MoonOut 上下文工程是什么?过时了么?一文讲明白! - 一枫说码 开了 TUN 模式还是直连?90% 的人都踩过这个坑 AScript扩展多种脚本语言 - rockey627 AI 学习笔记:Agent 的记忆机制 你能被装进一个文件里吗?——7 万人把同事"蒸馏"成了 AI - 我没有三颗心脏 Claude Code 通关手册(七):给 AI 装上技能包——Skills 完全指南 - 暮色之狐 在浏览器中快速编辑代码:VSCode Web 集成实践 - Newbe36524 蒸馏自己 skill?基于 Deepseek 的蒸馏器,丐版蒸馏方式,简单便捷 - To_Carpe_Diem Spring AI Aliababa和AgentScope,哪个更好? - 苏三说技术
筑基期:掌握Odoo基础核心知识点02(Odoo XML 开发方式详解)
okkk!!! · 2026-04-16 · via 博客园_首页

Odoo XML 开发方式详解

目录


1. record — 创建或覆盖记录

1.1 基本概念

<record> 是 Odoo XML 中最基础的操作,用于在数据库中创建覆盖一条记录。几乎所有其他标签(menuitem、act_window 等)本质上都是 record 的简写。

1.2 创建新记录

<record id="my_custom_action" model="ir.actions.act_window">
    <field name="name">My Custom Action</field>
    <field name="res_model">my.model</field>
    <field name="view_mode">tree,form</field>
</record>

要点:

  • id:只写名称(不带模块前缀),表示在当前模块中创建新记录
  • model:指定要创建记录的模型(数据库表)
  • <field>:设置该记录各个字段的值

1.3 覆盖已有记录

<!-- 覆盖 emabc_base 模块中定义的 to_approve_action -->
<record id="emabc_base.to_approve_action" model="ir.actions.act_window">
    <field name="context">{"search_default_not_approved":1, 'show_batch_without_transfer':True,
        "hide_hly_reimbursement": True}
    </field>
</record>

要点:

  • id 写成 模块名.外部ID(如 emabc_base.to_approve_action),Odoo 会找到该记录并更新,而不是新建
  • 只写需要修改的 <field>,未写的字段保持原值不变
  • 这是整体覆盖,写什么就改什么,没写的不管

1.4 实际项目中的例子

例1:覆盖 action 的 context

<record id="emabc_base.to_approve_action" model="ir.actions.act_window">
    <field name="context">{"search_default_not_approved":1, 'show_batch_without_transfer':True,
        "hide_hly_reimbursement": True}
    </field>
</record>

解读:

  • 找到 emabc_base 模块中 id 为 to_approve_actionir.actions.act_window 记录
  • 只修改它的 context 字段
  • 其他字段(name、res_model、domain、view_mode 等)保持原值

⚠️ 注意: 由于是覆盖而非继承,context 的内容必须完整重写。如果原 context 有 {"search_default_not_approved":1, "show_batch_without_transfer":True},而你只写了 {"hide_hly_reimbursement": True},那原来的内容就丢了。

例2:创建视图记录

<record id="expense_reimbursement_from_view" model="ir.ui.view">
    <field name="name">General Reimbursement</field>
    <field name="model">emabc.expense.reimbursement</field>
    <field name="inherit_id" ref="emabc_expense.expense_reimbursement_from_view"/>
    <field name="arch" type="xml">
        <!-- 这里放视图的 XML 结构 -->
    </field>
</record>

解读:

  • 创建一条 ir.ui.view 记录
  • inherit_id 指定继承哪个已有视图(配合 xpath 或字段定位使用)
  • arch 中定义视图的 XML 结构

1.5 field 的常用写法

<!-- 普通文本值 -->
<field name="name">My Name</field>

<!-- 布尔值 -->
<field name="active">True</field>

<!-- 整数值 -->
<field name="sequence">10</field>

<!-- 引用其他记录的 ID(Many2one 字段) -->
<field name="inherit_id" ref="emabc_expense.expense_reimbursement_from_view"/>
<!-- ref="模块名.外部ID" 引用其他模块中定义的记录 -->

<!-- eval 表达式 -->
<field name="context" eval="{'search_default_not_approved': 1}"/>
<!-- eval 中写 Python 表达式,值可以是字典、列表等 -->

<!-- domain 过滤 -->
<field name="domain">[('reimbursement_type_state', '=', 'project')]</field>

<!-- many2many / one2many 字段 -->
<field name="groups_id" eval="[(6,0,[ref('emabc_base.expense_group_loan_reimbursement')])]"/>
<!-- (6,0,[id1,id2]) = 替换整个列表 -->
<!-- (4,id) = 添加一条 -->
<!-- (3,id) = 删除一条 -->
<!-- (5,0,[]) = 清空所有 -->

2. xpath — 继承修改视图

2.1 基本概念

xpath 用于在已有的视图上做局部修改,而不是重写整个视图。这是 Odoo 中最常用的视图定制方式。

2.2 使用前提

必须配合 ir.ui.view 记录和 inherit_id 使用:

<record id="my_view_inherit" model="ir.ui.view">
    <field name="name">My View Inherit</field>
    <field name="model">emabc.expense.reimbursement</field>
    <field name="inherit_id" ref="emabc_expense.expense_reimbursement_from_view"/>
    <field name="arch" type="xml">
        <!-- xpath 写在这里面 -->
    </field>
</record>

2.3 xpath 语法

<xpath expr="定位表达式" position="操作方式">
    <!-- 要插入/替换的内容 -->
</xpath>
  • expr:XPath 表达式,定位到原视图中的某个节点
  • position:操作方式

2.4 position 操作方式详解

position 值 作用 说明
after 在定位节点后面插入 最常用,在某个字段后面加新字段
before 在定位节点前面插入 在某个字段前面加新字段
inside 在定位节点内部插入 在 group、page 等容器内追加内容
replace 替换定位节点 整个替换原来的元素
attributes 修改定位节点的属性 必须配合 <attribute> 标签使用

2.5 实际项目中的例子详解

例1:修改按钮属性(position="attributes")

<xpath expr="//button[@name='loan_reconcile_wizard']" position="attributes">
    <attribute name="attrs">{'invisible':['|', ('state','not in',['financial_audit','partly_paid']), ('is_corporate_import', '=', True)]}</attribute>
</xpath>

逐步解读:

  1. expr="//button[@name='loan_reconcile_wizard']" — 找到原视图中 name 为 loan_reconcile_wizard 的 button

    • // 表示在任意层级查找
    • [@name='xxx'] 是属性选择器,筛选 name 属性等于 xxx 的元素
  2. position="attributes" — 要修改这个按钮的属性

  3. <attribute name="attrs">...</attribute> — 修改 attrs 属性的值为新的表达式

效果: 让"借款核销"按钮在"对公账单导入"时隐藏

例2:在字段后面插入新字段(position="after")

<xpath expr="//field[@name='actual_payee_bank']" position="after">
    <field name="is_corporate_import" readonly="1" options="{'readonly_save':True}"/>
</xpath>

逐步解读:

  1. expr="//field[@name='actual_payee_bank']" — 找到 name 为 actual_payee_bank 的 field
  2. position="after" — 在它后面插入
  3. 插入一个新的 is_corporate_import 字段

效果: 在"实际收款银行"字段后面添加"对公账单导入"字段

2.6 常用 xpath 表达式

<!-- 找 name 为 xxx 的 field -->
<xpath expr="//field[@name='xxx']" ...>

<!-- 找 name 为 xxx 的 button -->
<xpath expr="//button[@name='xxx']" ...>

<!-- 找 string 为 xxx 的 page -->
<xpath expr="//page[@string='xxx']" ...>

<!-- 找 name 为 xxx 的 group -->
<xpath expr="//group[@name='xxx']" ...>

<!-- 找第一个 form 下的 header -->
<xpath expr="//form/header" ...>

<!-- 找 notebook 下的第一个 page -->
<xpath expr="//notebook/page[1]" ...>

<!-- 找有特定 class 的元素 -->
<xpath expr="//div[@class='oe_title']" ...>

<!-- 组合条件:找 form 下 name 为 xxx 的 field -->
<xpath expr="//form//field[@name='xxx']" ...>

2.7 多个 xpath 修改同一个视图

一个继承视图中可以写多个 xpath,每个修改不同位置:

<record id="expense_reimbursement_from_view" model="ir.ui.view">
    <field name="name">General Reimbursement</field>
    <field name="model">emabc.expense.reimbursement</field>
    <field name="inherit_id" ref="emabc_expense.expense_reimbursement_from_view"/>
    <field name="arch" type="xml">
        <!-- 第1处修改:改按钮属性 -->
        <xpath expr="//button[@name='loan_reconcile_wizard']" position="attributes">
            <attribute name="attrs">...</attribute>
        </xpath>

        <!-- 第2处修改:改另一个按钮属性 -->
        <xpath expr="//button[@name='registered_payment_action']" position="attributes">
            <attribute name="attrs">...</attribute>
        </xpath>

        <!-- 第3处修改:在字段后面插入新字段 -->
        <xpath expr="//field[@name='actual_payee_bank']" position="after">
            <field name="is_corporate_import" readonly="1"/>
        </xpath>
    </field>
</record>

3. 字段定位 — xpath 的简化写法

3.1 基本概念

当你要定位的节点是 <field> 时,可以省略 xpath,直接用 <field name="xxx"> 来定位。

3.2 语法

<field name="要定位的字段名" position="操作方式">
    <!-- 插入的内容 -->
</field>

3.3 对比 xpath

<!-- xpath 写法 -->
<xpath expr="//field[@name='actual_payee_bank']" position="after">
    <field name="is_corporate_import" readonly="1"/>
</xpath>

<!-- 字段定位写法(效果完全相同,但更简洁) -->
<field name="actual_payee_bank" position="after">
    <field name="is_corporate_import" readonly="1"/>
</field>

3.4 限制

  • 只能定位 <field> 节点,不能定位 button、group、page、notebook 等非 field 元素
  • 对于非 field 元素,必须用 xpath

3.5 各种 position 的字段定位写法

<!-- 在某字段后面插入 -->
<field name="actual_payee_bank" position="after">
    <field name="is_corporate_import"/>
</field>

<!-- 在某字段前面插入 -->
<field name="actual_payee_bank" position="before">
    <field name="new_field_before"/>
</field>

<!-- 替换某字段 -->
<field name="old_field" position="replace">
    <field name="new_field"/>
</field>

<!-- 修改某字段的属性 -->
<field name="my_field" position="attributes">
    <attribute name="invisible">1</attribute>
    <attribute name="readonly">1</attribute>
</field>

4. delete — 删除已有记录

4.1 基本概念

<delete> 用于在模块安装/更新时删除数据库中已有的记录。

4.2 语法

<delete id="模块名.外部ID" model="模型名"/>

或者用 search 条件删除:

<delete model="模型名" search="[(搜索条件)]"/>

4.3 实际项目中的例子

<!-- emabc_bpm_interface 模块中 -->
<delete id="emabc_base.to_approve_action" model="ir.actions.act_window"/>

<!-- 然后重新创建一个新的同名 action -->
<record id="to_approve_action" model="ir.actions.act_window">
    <field name="name">To Approve</field>
    <field name="res_model">emabc.base.wkf.task</field>
    <field name="view_type">form</field>
    <field name="view_mode">tree</field>
    <field name="context">{"search_default_not_approved":1, 'show_batch_without_transfer':True}</field>
    <field name="domain">...</field>
</record>

解读:

  1. 先用 <delete> 删掉 emabc_base 模块中原来的 to_approve_action
  2. 再用 <record> 重新创建一个全新的 to_approve_action(注意此时 id 不带模块前缀,因为是在当前模块新建)

4.4 delete vs record 覆盖

<delete> + 新建 <record> 覆盖
原记录 彻底删除,重建 保留,只修改指定字段
未写的字段 需要全部重新写 保持原值
风险 漏写字段会丢失数据 只影响写的字段
适用场景 需要大幅改造,几乎重写 只需修改少量字段

5.1 基本概念

快捷标签是 <record> 的简化写法,Odoo 内部会自动将它们转换为 record。用哪种写法效果完全一样,快捷标签只是代码更短。

<!-- 快捷写法 -->
<menuitem name="差旅账单导入"
          id="hly_import_expense_wizard_menu"
          sequence="50"
          action="hly_import_expense_wizard_action"
          parent="emabc_expense.expense_reimbursement_menu"
          groups="emabc_base.expense_group_loan_reimbursement"/>

<!-- 等价的 record 写法 -->
<record id="hly_import_expense_wizard_menu" model="ir.ui.menu">
    <field name="name">差旅账单导入</field>
    <field name="sequence">50</field>
    <field name="action" ref="hly_import_expense_wizard_action"/>
    <field name="parent_id" ref="emabc_expense.expense_reimbursement_menu"/>
    <field name="groups_id" eval="[(6,0,[ref('emabc_base.expense_group_loan_reimbursement')])]"/>
</record>

menuitem 常用属性:

属性 说明
name 菜单显示名称
id 外部 ID
parent 父菜单的 xml_id
action 点击菜单时执行的 action 的 xml_id
sequence 排序序号(数字越小越靠前)
groups 权限组的 xml_id(多个用逗号分隔)

5.3 act_window

<!-- 快捷写法 -->
<act_window id="my_action"
            name="My Action"
            res_model="my.model"
            view_mode="tree,form"/>

<!-- 等价的 record 写法 -->
<record id="my_action" model="ir.actions.act_window">
    <field name="name">My Action</field>
    <field name="res_model">my.model</field>
    <field name="view_mode">tree,form</field>
</record>

5.4 report

<!-- 快捷写法 -->
<report id="my_report"
        name="My Report"
        model="my.model"
        report_type="qweb-pdf"
        file="my_module.report_template"/>

<!-- 等价的 record 写法 -->
<record id="my_report" model="ir.actions.report.xml">
    <field name="name">My Report</field>
    <field name="model">my.model</field>
    <field name="report_type">qweb-pdf</field>
    <field name="report_name">my_module.report_template</field>
</record>

5.5 template(QWeb 视图)

<!-- 快捷写法 -->
<template id="my_template" name="My Template">
    <t t-call="website.layout">
        <div>Content here</div>
    </t>
</template>

<!-- 等价的 record 写法 -->
<record id="my_template" model="ir.ui.view">
    <field name="name">My Template</field>
    <field name="type">qweb</field>
    <field name="arch" type="xml">
        <t t-call="website.layout">
            <div>Content here</div>
        </t>
    </field>
</record>

6. function — 调用 Python 方法

6.1 基本概念

<function> 用于在模块安装/更新时自动调用 Python 方法,通常用于初始化数据。

6.2 语法

<function model="模型名" name="方法名" eval="参数表达式"/>

6.3 示例

<!-- 调用 res.partner 模型的 name_search 方法,传入参数 ['abc'] -->
<function model="res.partner" name="name_search" eval="['abc']"/>

<!-- 调用当前模型的方法,不传参数 -->
<function model="my.model" name="my_init_method"/>

<!-- 传入多个参数 -->
<function model="my.model" name="my_method" eval="[ref('module.xml_id'), 'some_string']"/>

6.4 常见用途

  • 模块安装时初始化数据
  • 批量更新已有记录
  • 执行数据迁移逻辑

7. 各种方式的对比总结

7.1 选择决策树

你需要做什么?
│
├── 创建新记录(菜单、action、视图等)
│   ├── 是菜单 → 用 <menuitem>
│   ├── 是 action → 用 <act_window> 或 <record>
│   ├── 是报表 → 用 <report>
│   ├── 是 QWeb 模板 → 用 <template>
│   └── 其他 → 用 <record>
│
├── 修改已有记录的字段值
│   ├── 是视图的 arch 结构 → 用 xpath 或字段定位
│   └── 是其他字段(context、domain 等)→ 用 <record> 覆盖
│
├── 删除已有记录 → 用 <delete>
│
└── 安装时执行方法 → 用 <function>

7.2 视图修改方式对比

方式 能定位的元素 代码量 使用前提
xpath 任意元素(button、group、page、field 等) 较多 需要 inherit_id
字段定位 <field> 元素 较少 需要 inherit_id
record 覆盖整个 arch 不需要定位 重写整个视图 不需要 inherit_id,但风险大

7.3 记录修改方式对比

方式 作用 风险
record 覆盖 修改指定字段,未写的保持原值 低,但 context 等整体字段需完整重写
delete + 新建 彻底删除重建 高,漏写字段会丢失数据
xpath/字段定位 在原视图上做局部增删改 低,不影响未修改部分

7.4 xml_id 的写法与行为

id 写法 含义 行为
id="my_id" 当前模块的外部 ID 在当前模块创建新记录
id="other_module.existing_id" 其他模块的外部 ID 找到该记录并覆盖更新
ref="other_module.existing_id" 在 field 中引用其他模块的记录 获取该记录的数据库 ID