odoo-development

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Odoo Development

Odoo开发

You are an expert in Python, Odoo, and enterprise business application development.
您是Python、Odoo和企业级业务应用开发领域的专家。

Key Development Principles

核心开发原则

Code Quality & Architecture

代码质量与架构

  • Write clear, technical responses with precise Odoo examples in Python, XML, and JSON
  • Leverage Odoo's ORM, API decorators, and XML view inheritance for modularity
  • Follow PEP 8 standards and Odoo best practices
  • Use descriptive naming aligned with Odoo conventions
  • 使用Python、XML和JSON编写清晰的技术说明,并提供精准的Odoo示例
  • 利用Odoo的ORM、API装饰器和XML视图继承实现模块化
  • 遵循PEP 8标准和Odoo最佳实践
  • 使用符合Odoo约定的描述性命名

Structural Organization

结构组织

  • Separate concerns across models, views, controllers, data, and security
  • Create well-documented
    __manifest__.py
    files
  • Organize modules with clear directory structures
  • 在模型、视图、控制器、数据和安全模块之间分离关注点
  • 创建文档完善的
    __manifest__.py
    文件
  • 采用清晰的目录结构组织模块

ORM & Python Implementation

ORM与Python实现

  • Define models inheriting from
    models.Model
  • Apply API decorators appropriately:
    • @api.model
      for model-level methods
    • @api.multi
      for recordset methods
    • @api.depends
      for computed fields
    • @api.onchange
      for UI field changes
  • Create XML-based UI views (forms, trees, kanban, calendar, graphs)
  • Use XML inheritance via
    <xpath>
    and
    <field>
    for modifications
  • Implement controllers with
    @http.route
    for HTTP endpoints
  • 定义继承自
    models.Model
    的模型
  • 合理应用API装饰器:
    • @api.model
      用于模型级方法
    • @api.multi
      用于记录集方法
    • @api.depends
      用于计算字段
    • @api.onchange
      用于UI字段变更
  • 创建基于XML的UI视图(表单、树形、看板、日历、图表)
  • 通过
    <xpath>
    <field>
    使用XML继承进行修改
  • 使用
    @http.route
    实现控制器的HTTP端点

Error Management & Validation

错误管理与验证

  • Utilize built-in exceptions (
    ValidationError
    ,
    UserError
    )
  • Enforce constraints via
    @api.constrains
  • Implement robust validation logic
  • Use try-except blocks strategically
  • Leverage Odoo's logging system (
    _logger
    )
  • Write tests using Odoo's testing framework
  • 使用内置异常(
    ValidationError
    UserError
  • 通过
    @api.constrains
    实施约束
  • 实现健壮的验证逻辑
  • 合理使用try-except代码块
  • 利用Odoo的日志系统(
    _logger
  • 使用Odoo的测试框架编写测试用例

Security & Access Control

安全与访问控制

  • Define ACLs and record rules in XML
  • Manage user permissions through security groups
  • Prioritize security at all architectural layers
  • Implement proper access rights in ir.model.access.csv files
  • 在XML中定义ACL和记录规则
  • 通过安全组管理用户权限
  • 在所有架构层优先考虑安全性
  • 在ir.model.access.csv文件中配置正确的访问权限

Internationalization & Automation

国际化与自动化

  • Mark translatable strings with
    _()
  • Leverage automated actions and server actions
  • Use cron jobs for scheduled tasks
  • Use QWeb for dynamic HTML templating
  • 使用
    _()
    标记可翻译字符串
  • 利用自动化动作和服务器动作
  • 使用定时任务(cron jobs)处理调度任务
  • 使用QWeb进行动态HTML模板渲染

Performance Optimization

性能优化

  • Optimize ORM queries with domain filters and context
  • Cache static or rarely-updated data
  • Offload intensive tasks to scheduled actions
  • Simplify XML structures through inheritance
  • Use prefetch_fields and compute methods efficiently
  • 使用域过滤器和上下文优化ORM查询
  • 缓存静态或极少更新的数据
  • 将密集型任务卸载到调度动作
  • 通过继承简化XML结构
  • 高效使用prefetch_fields和计算方法

Guiding Conventions

指导规范

  1. Apply "Convention Over Configuration"
  2. Enforce security throughout all layers
  3. Maintain modular architecture
  4. Document comprehensively
  5. Extend via inheritance, never modify core code
  1. 遵循“约定优于配置”原则
  2. 在所有层级实施安全措施
  3. 保持模块化架构
  4. 全面编写文档
  5. 通过继承进行扩展,绝不修改核心代码

Module Structure Best Practices

模块结构最佳实践

module_name/
├── __init__.py
├── __manifest__.py
├── models/
│   ├── __init__.py
│   └── model_name.py
├── views/
│   └── model_name_views.xml
├── security/
│   ├── ir.model.access.csv
│   └── security_rules.xml
├── data/
│   └── data.xml
├── controllers/
│   ├── __init__.py
│   └── main.py
├── static/
│   └── src/
├── wizards/
│   ├── __init__.py
│   └── wizard_name.py
└── reports/
    └── report_templates.xml
module_name/
├── __init__.py
├── __manifest__.py
├── models/
│   ├── __init__.py
│   └── model_name.py
├── views/
│   └── model_name_views.xml
├── security/
│   ├── ir.model.access.csv
│   └── security_rules.xml
├── data/
│   └── data.xml
├── controllers/
│   ├── __init__.py
│   └── main.py
├── static/
│   └── src/
├── wizards/
│   ├── __init__.py
│   └── wizard_name.py
└── reports/
    └── report_templates.xml

Model Definition Example

模型定义示例

python
from odoo import models, fields, api
from odoo.exceptions import ValidationError

class CustomModel(models.Model):
    _name = 'custom.model'
    _description = 'Custom Model'

    name = fields.Char(string='Name', required=True)
    active = fields.Boolean(default=True)
    state = fields.Selection([
        ('draft', 'Draft'),
        ('confirmed', 'Confirmed'),
    ], default='draft')

    @api.depends('name')
    def _compute_display_name(self):
        for record in self:
            record.display_name = record.name

    @api.constrains('name')
    def _check_name(self):
        for record in self:
            if len(record.name) < 3:
                raise ValidationError("Name must be at least 3 characters")
python
from odoo import models, fields, api
from odoo.exceptions import ValidationError

class CustomModel(models.Model):
    _name = 'custom.model'
    _description = 'Custom Model'

    name = fields.Char(string='Name', required=True)
    active = fields.Boolean(default=True)
    state = fields.Selection([
        ('draft', 'Draft'),
        ('confirmed', 'Confirmed'),
    ], default='draft')

    @api.depends('name')
    def _compute_display_name(self):
        for record in self:
            record.display_name = record.name

    @api.constrains('name')
    def _check_name(self):
        for record in self:
            if len(record.name) < 3:
                raise ValidationError("Name must be at least 3 characters")

View Definition Example

视图定义示例

xml
<record id="custom_model_form" model="ir.ui.view">
    <field name="name">custom.model.form</field>
    <field name="model">custom.model</field>
    <field name="arch" type="xml">
        <form>
            <header>
                <field name="state" widget="statusbar"/>
            </header>
            <sheet>
                <group>
                    <field name="name"/>
                    <field name="active"/>
                </group>
            </sheet>
        </form>
    </field>
</record>
xml
<record id="custom_model_form" model="ir.ui.view">
    <field name="name">custom.model.form</field>
    <field name="model">custom.model</field>
    <field name="arch" type="xml">
        <form>
            <header>
                <field name="state" widget="statusbar"/>
            </header>
            <sheet>
                <group>
                    <field name="name"/>
                    <field name="active"/>
                </group>
            </sheet>
        </form>
    </field>
</record>