跳到正文
Skillpedia
API 与服务器(后端)48k · #5345

odoo-orm-expert

它能做什么

这个工具帮你使用 Odoo ORM,也就是 Odoo 与数据库交互的方式。它会教你如何查询、创建、修改和删除数据,以及如何优化性能。

适用场景

  • ✓需要在 Odoo 中查找或修改信息时。
  • ✓需要创建复杂过滤条件来查看数据时。
  • ✓想提升 Odoo 操作速度时。

技能链接

https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/odoo-orm-expert/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/odoo-orm-expert/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“帮我写出查找今年下过订单的所有巴西客户的代码。”

每周 5 个新技能,1 分钟读完。

工作原理

你向 AI 提问时提到这个工具的名称即可。AI 会给出正确的 Odoo 代码,并解释其工作原理。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Odoo ORM Expert

Overview

This skill teaches you Odoo's Object Relational Mapper (ORM) in depth. It covers reading/writing records, building domain filters, working with relational fields, and avoiding common performance pitfalls like N+1 queries.

When to Use This Skill

  • Writing search(), browse(), create(), write(), or unlink() calls.
  • Building complex domain filters for views or server actions.
  • Implementing computed, stored, and related fields.
  • Debugging slow queries or optimizing bulk operations.

How It Works

  1. Activate: Mention @odoo-orm-expert and describe what data operation you need.
  2. Get Code: Receive correct, idiomatic Odoo ORM code with explanations.
  3. Optimize: Ask for performance review on existing ORM code.

Examples

Example 1: Search with Domain Filters

# Find all confirmed sale orders for a specific customer, created this year
import datetime

start_of_year = datetime.date.today().replace(month=1, day=1).strftime('%Y-%m-%d')

orders = self.env['sale.order'].search([
    ('partner_id', '=', partner_id),
    ('state', '=', 'sale'),
    ('date_order', '>=', start_of_year),
], order='date_order desc', limit=50)

# Note: pass dates as 'YYYY-MM-DD' strings in domains,
# NOT as fields.Date objects — the ORM serializes them correctly.

Example 2: Computed Field

total_order_count = fields.Integer(
    string='Total Orders',
    compute='_compute_total_order_count',
    store=True
)

@api.depends('sale_order_ids')
def _compute_total_order_count(self):
    for record in self:
        record.total_order_count = len(record.sale_order_ids)

Example 3: Safe Bulk Write (avoid N+1)

# Batch writes reduce repeated calls, but can trigger additional compute/tracking queries
partners = self.env['res.partner'].browse(approved_partner_ids).exists()  # Explicit scoped IDs
partners.write({'country_id': self.env.ref('base.us').id})

# ❌ BAD: Triggers a separate query per record
for partner in partners:
    partner.country_id = self.env.ref('base.us').id

Best Practices

  • ✅ Do: Use mapped(), filtered(), and sorted() on recordsets instead of Python loops.
  • ✅ Do: Use sudo() sparingly and only when you understand the security implications.
  • ✅ Do: Prefer search_count() over len(search(...)) when you only need a count.
  • ✅ Do: Use with_context(...) to pass context values cleanly rather than modifying self.env.context directly.
  • ❌ Don't: Call search() inside a loop — this is the #1 Odoo performance killer.
  • ❌ Don't: Use raw SQL unless absolutely necessary; use ORM for all standard operations.
  • ❌ Don't: Pass Python datetime/date objects directly into domain tuples — always stringify them as 'YYYY-MM-DD'.

Limitations

  • Does not cover cr.execute() raw SQL patterns in depth — use the Odoo performance tuner skill for SQL-level optimization.
  • Stored computed fields can cause significant write overhead at scale; this skill does not cover partitioning strategies.
  • Does not cover transient models (models.TransientModel) or wizard patterns.
  • ORM behavior can differ slightly between Odoo SaaS and On-Premise due to config overrides.

Examples are model-method fragments requiring imports, record definitions and authorized scope. Do not infer missing country data means US; write only confirmed values to selected records. sudo() bypasses access rules and requires a justified boundary. Batch operations do not guarantee one SQL query, and mapped/filtered may load large recordsets; profile actual query counts and test record-rule/multi-company isolation.

常见问题

什么是 Odoo ORM?

它是 Odoo 组织数据并访问数据库的方式,就像一个翻译,把你的需求转换成数据库能理解的语言。

什么是“域过滤器”(domain filters)?

它们是你设置的筛选规则,用来决定要看哪些数据,比如“只显示活跃客户”。

为什么要优化性能?

让 Odoo 运行得更快,尤其是数据量大或用户多的时候。

详情

作者的原始描述

Master Odoo ORM patterns: search, browse, create, write, domain filters, computed fields, and performance-safe query techniques.

repo: sickn33/agentic-awesome-skills

热度

GitHub 星标
48k
排名
#5345
许可证
MIT
更新时间
2026/10/15

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能