odoo-17.0
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseOdoo 17.0 Development Skill
Odoo 17.0 开发技能
A comprehensive skill set for building production-quality Odoo 17.0 custom
modules. Covers the full development lifecycle: module structure, ORM, views,
actions, performance, and testing — following official Odoo coding guidelines.
一套用于构建生产级Odoo 17.0自定义模块的全面技能集合。涵盖完整开发生命周期:模块结构、ORM、视图、动作、性能及测试——遵循Odoo官方编码规范。
When to Apply
适用场景
Reference these rules when:
- Creating or extending a custom Odoo 17.0 module
- Defining models, fields, or compute methods
- Writing decorators, constraints, or onchange handlers
@api - Building XML views, menus, and actions
- Optimizing queries or processing large recordsets
- Writing unit tests, integration tests, or UI tours
- Adding chatter, sequences, ratings, or portal access to a model
在以下场景中参考这些规则:
- 创建或扩展Odoo 17.0自定义模块
- 定义模型、字段或计算方法
- 编写装饰器、约束或onchange处理器
@api - 构建XML视图、菜单与动作
- 优化查询或处理大型记录集
- 编写单元测试、集成测试或UI向导
- 为模型添加 chatter、序列、评分或门户访问权限
Rule Categories by Priority
按优先级划分的规则类别
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Module Manifest | HIGH | |
| 2 | ORM Models | HIGH | |
| 3 | ORM Fields | HIGH | |
| 4 | API Decorators | HIGH | |
| 5 | Recordset & Domains | MEDIUM | |
| 6 | Actions | MEDIUM | |
| 7 | Performance | MEDIUM | |
| 8 | Testing | MEDIUM | |
| 9 | Mixins | LOW | |
| Priority | Category | 影响程度 | Prefix |
|---|---|---|---|
| 1 | Module Manifest | 高 | |
| 2 | ORM Models | 高 | |
| 3 | ORM Fields | 高 | |
| 4 | API Decorators | 高 | |
| 5 | Recordset & Domains | 中 | |
| 6 | Actions | 中 | |
| 7 | Performance | 中 | |
| 8 | Testing | 中 | |
| 9 | Mixins | 低 | |
Quick Reference
快速参考
1. Module Manifest (HIGH)
1. Module Manifest(高优先级)
- — Every module must have
manifest-required-keys,name,version,depends,licenseinstallable - — Security CSV always first in
manifest-data-order, menus always lastdata - — Register JS/OWL/SCSS files under
manifest-assets-bundleassets.web.assets_backend - — Use
manifest-version-formate.g.{odoo}.{major}.{minor}.{patch}17.0.1.0.0 - — Missing or wrong
manifest-dependenciesprevents module installationdepends
- — 每个模块必须包含
manifest-required-keys、name、version、depends、licenseinstallable - — 安全CSV文件始终在
manifest-data-order中排首位,菜单始终排最后data - — 在
manifest-assets-bundle下注册JS/OWL/SCSS文件assets.web.assets_backend - — 使用
manifest-version-format格式,例如{odoo}.{major}.{minor}.{patch}17.0.1.0.0 - — 缺失或错误的
manifest-dependencies会导致模块无法安装depends
2. ORM Models (HIGH)
2. ORM Models(高优先级)
- — Follow canonical attribute → field → constraint → compute → CRUD → action → helper order
models-class-order - — Use
models-type-selectionfor persistence,Modelfor wizards,TransientModelfor mixinsAbstractModel - — Know the 4
models-inherit-modesmodes: extend, copy, compose, delegate_inherit - — Prefer
models-sql-constraintsover Python for uniqueness checks_sql_constraints - —
models-required-attributesand_nameare required on every new model_description
- — 遵循规范的属性→字段→约束→计算→CRUD→动作→辅助方法顺序
models-class-order - — 持久化模型使用
models-type-selection,向导使用Model,混合类使用TransientModelAbstractModel - — 了解4种
models-inherit-modes模式:扩展、复制、组合、委托_inherit - — 优先使用
models-sql-constraints而非Python代码进行唯一性校验_sql_constraints - — 每个新模型都必须包含
models-required-attributes和_name_description
3. ORM Fields (HIGH)
3. ORM Fields(高优先级)
- —
fields-monetary-currencyfields always require a pairedMonetarycurrency_field - — Always set
fields-many2one-indexonindex=Truefields used in searchesMany2one - — Use
fields-store-decisiononly when field needs to be searched or sortedstore=True - — Use ORM write commands
fields-m2m-commands(0,0,v)(4,id)for relational writes(6,0,ids) - — When extending a
fields-selection-add, always defineSelectionfor new keysondelete
- —
fields-monetary-currency字段必须始终配对Monetarycurrency_field - — 用于搜索的
fields-many2one-index字段始终设置Many2oneindex=True - — 仅当字段需要被搜索或排序时才使用
fields-store-decisionstore=True - — 使用ORM写入命令
fields-m2m-commands(0,0,v)(4,id)进行关联写入(6,0,ids) - — 扩展
fields-selection-add时,必须为新键定义Selectionondelete
4. API Decorators (HIGH)
4. API Decorators(高优先级)
- — List ALL fields that affect the computed result
decorators-depends-completeness - — Always assign computed field in every code path
decorators-depends-assign-all-branches - —
decorators-constrains-exceptionmust raise@api.constrains, neverValidationErrorUserError - — Always loop
decorators-constrains-loopinsidefor rec in self@api.constrains - —
decorators-onchange-not-enforceddoes not run on programmatic create/write@api.onchange - —
decorators-onchange-newidinsideself.idis a@api.onchange, not an integerNewId - — Always use
decorators-create-multifor@api.model_create_multioverrides in 17.0create() - — Fields with
decorators-depends-context-no-storecannot be@api.depends_contextstore=True
- — 列出所有影响计算结果的字段
decorators-depends-completeness - — 在所有代码分支中都必须为计算字段赋值
decorators-depends-assign-all-branches - —
decorators-constrains-exception必须抛出@api.constrains,绝不能抛出ValidationErrorUserError - — 在
decorators-constrains-loop内部必须始终遍历@api.constrainsfor rec in self - —
decorators-onchange-not-enforced不会在程序化创建/写入时运行@api.onchange - —
decorators-onchange-newid内部的@api.onchange是self.id,而非整数NewId - — 在17.0版本中,重写
decorators-create-multi时必须始终使用create()@api.model_create_multi - — 使用
decorators-depends-context-no-store的字段不能设置@api.depends_contextstore=True
5. Recordset & Domains (MEDIUM)
5. Recordset & Domains(中优先级)
- — Use
recordsets-search-countinstead ofsearch_count()for countinglen(search()) - — Call
recordsets-batch-writeon the whole recordset, never inside a loopwrite() - — Pass a list to
recordsets-batch-createinstead of calling it in a loopcreate([...]) - —
recordsets-domain-operatorsand|are prefix operators applying to the next two conditions& - — Always add a comment explaining why
recordsets-sudo-commentis usedsudo() - — Never format SQL strings; always use
recordsets-raw-sql-paramsparameterized queries%s
- — 计数时使用
recordsets-search-count而非search_count()len(search()) - — 对整个记录集调用
recordsets-batch-write,绝不在循环内调用write() - — 传入列表
recordsets-batch-create而非在循环内调用create([...])create() - —
recordsets-domain-operators和|是前缀运算符,作用于后续两个条件& - — 使用
recordsets-sudo-comment时必须添加注释说明原因sudo() - — 绝不能格式化SQL字符串;始终使用
recordsets-raw-sql-params参数化查询%s
6. Actions (MEDIUM)
6. Actions(中优先级)
- — Always bind leaf
actions-window-menuto an<menuitem>recordir.actions.act_window - — Declare actions in view XML, menus in a separate
actions-data-orderloaded lastmenus.xml - — Return action dicts from Python methods to open views or wizards
actions-python-return - — Cron methods must be decorated with
actions-cron-methodand handle their own errors@api.model - — Use
actions-server-bindingto add server actions to the Action dropdownbinding_model_id
- — 始终将叶子节点
actions-window-menu绑定到<menuitem>记录ir.actions.act_window - — 在视图XML中声明动作,菜单在单独的
actions-data-order中声明并最后加载menus.xml - — 从Python方法返回动作字典以打开视图或向导
actions-python-return - — Cron方法必须使用
actions-cron-method装饰,并自行处理错误@api.model - — 使用
actions-server-binding将服务器动作添加到动作下拉菜单binding_model_id
7. Performance (MEDIUM)
7. Performance(中优先级)
- — Rely on ORM prefetch; use
performance-prefetchto extract cross-record field valuesmapped() - — Never call
performance-no-loop-writeorwrite()inside acreate()loopfor - — Add
performance-index-searched-fieldson fields used inindex=Truedomains orsearch()ORDER BY - — Use
performance-read-groupfor aggregations instead of loading full recordsetsread_group() - — Call
performance-invalidate-after-sqlafter any raw SQL writeinvalidate_model()
- — 依赖ORM预取;使用
performance-prefetch提取跨记录字段值mapped() - — 绝不在
performance-no-loop-write循环内调用for或write()create() - — 在用于
performance-index-searched-fields域或search()的字段上添加ORDER BYindex=True - — 聚合操作使用
performance-read-group而非加载完整记录集read_group() - — 执行任何原生SQL写入后调用
performance-invalidate-after-sqlinvalidate_model()
8. Testing (MEDIUM)
8. Testing(中优先级)
- — Use
testing-setup-classfor shared fixtures;setUpClassonly when full isolation is neededsetUp - — Tag tests with
testing-post-install-tag@tagged('post_install', '-at_install') - — Every
testing-constrains-coveragemethod must have a test that triggers it@api.constrains - — Test wizards by creating them with context, then calling the action method
testing-wizard-pattern - — UI tours must be registered in the
testing-tour-jsregistry and tested viaweb_tour.toursHttpCase
- — 共享测试夹具使用
testing-setup-class;仅在需要完全隔离时使用setUpClasssetUp - — 测试用
testing-post-install-tag标记@tagged('post_install', '-at_install') - — 每个
testing-constrains-coverage方法都必须有触发它的测试用例@api.constrains - — 通过传入上下文创建向导,再调用动作方法进行测试
testing-wizard-pattern - — UI向导必须注册到
testing-tour-js注册表,并通过web_tour.tours测试HttpCase
9. Mixins (LOW)
9. Mixins(低优先级)
- — Add
mixins-mail-threadfor chatter + activities_inherit = ['mail.thread', 'mail.activity.mixin'] - — Use
mixins-trackingon fields to log changes in the chatter automaticallytracking=True - — Use
mixins-sequence+ir.sequencein fieldnext_by_code()for auto-numberingdefault= - — Implement
mixins-portalwhen using_compute_access_urlportal.mixin - — Use
mixins-message-postfor programmatic chatter messagesmessage_post()
- — 添加
mixins-mail-thread以实现chatter + 活动功能_inherit = ['mail.thread', 'mail.activity.mixin'] - — 在字段上设置
mixins-tracking以自动在chatter中记录变更tracking=True - — 使用
mixins-sequence+ir.sequence作为字段next_by_code()实现自动编号default= - — 使用
mixins-portal时需实现portal.mixin_compute_access_url - — 程序化发送chatter消息使用
mixins-message-postmessage_post()
How to Use
使用方法
Read individual skill files for detailed explanations, correct and incorrect
code examples, and gotchas:
odoo-manifest.md
odoo-orm-models.md
odoo-orm-fields.md
odoo-orm-decorators.md
odoo-orm-recordsets.md
odoo-actions.md
odoo-performance.md
odoo-testing.md
odoo-mixins.mdEach file contains:
- Why it matters in Odoo 17.0
- Incorrect code examples with explanation ()
# ❌ WRONG - Correct code examples with explanation ()
# ✅ CORRECT - Decision trees and edge cases
阅读单个技能文件以获取详细说明、正确与错误的代码示例及注意事项:
odoo-manifest.md
odoo-orm-models.md
odoo-orm-fields.md
odoo-orm-decorators.md
odoo-orm-recordsets.md
odoo-actions.md
odoo-performance.md
odoo-testing.md
odoo-mixins.md每个文件包含:
- 该规则在Odoo 17.0中的重要性
- 错误代码示例及解释()
# ❌ WRONG - 正确代码示例及解释()
# ✅ CORRECT - 决策树与边缘情况
Full Compiled Document
完整编译文档
For the complete guide with all rules, patterns, and examples expanded,
including global coding guidelines and skill routing table:
AGENTS.md如需包含所有规则、模式及扩展示例的完整指南,包括全局编码规范与技能路由表,请查看:
AGENTS.md