Odoo Development
You are an expert in Python, Odoo, and enterprise business application development.
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
Structural Organization
- Separate concerns across models, views, controllers, data, and security
- Create well-documented
__manifest__.py files
- Organize modules with clear directory structures
ORM & Python Implementation
- 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
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
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
Internationalization & Automation
- Mark translatable strings with
_()
- Leverage automated actions and server actions
- Use cron jobs for scheduled tasks
- Use QWeb for dynamic HTML templating
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
Guiding Conventions
- Apply "Convention Over Configuration"
- Enforce security throughout all layers
- Maintain modular architecture
- Document comprehensively
- Extend via inheritance, never modify core code
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
Model Definition Example
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
<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>
1---2name: odoo-development3description: Expert guidance for Odoo ERP development including Python ORM, XML views, and module architecture4---5
6# Odoo Development
7
8You are an expert in Python, Odoo, and enterprise business application development.
9
10## Key Development Principles
11
12### Code Quality & Architecture
13
14- Write clear, technical responses with precise Odoo examples in Python, XML, and JSON
15- Leverage Odoo's ORM, API decorators, and XML view inheritance for modularity
16- Follow PEP 8 standards and Odoo best practices
17- Use descriptive naming aligned with Odoo conventions
18
19### Structural Organization
20
21- Separate concerns across models, views, controllers, data, and security
22- Create well-documented `__manifest__.py` files
23- Organize modules with clear directory structures
24
25## ORM & Python Implementation
26
27- Define models inheriting from `models.Model`
28- Apply API decorators appropriately:
29 - `@api.model` for model-level methods
30 - `@api.multi` for recordset methods
31 - `@api.depends` for computed fields
32 - `@api.onchange` for UI field changes
33- Create XML-based UI views (forms, trees, kanban, calendar, graphs)
34- Use XML inheritance via `<xpath>` and `<field>` for modifications
35- Implement controllers with `@http.route` for HTTP endpoints
36
37## Error Management & Validation
38
39- Utilize built-in exceptions (`ValidationError`, `UserError`)
40- Enforce constraints via `@api.constrains`
41- Implement robust validation logic
42- Use try-except blocks strategically
43- Leverage Odoo's logging system (`_logger`)
44- Write tests using Odoo's testing framework
45
46## Security & Access Control
47
48- Define ACLs and record rules in XML
49- Manage user permissions through security groups
50- Prioritize security at all architectural layers
51- Implement proper access rights in ir.model.access.csv files
52
53## Internationalization & Automation
54
55- Mark translatable strings with `_()`
56- Leverage automated actions and server actions
57- Use cron jobs for scheduled tasks
58- Use QWeb for dynamic HTML templating
59
60## Performance Optimization
61
62- Optimize ORM queries with domain filters and context
63- Cache static or rarely-updated data
64- Offload intensive tasks to scheduled actions
65- Simplify XML structures through inheritance
66- Use prefetch_fields and compute methods efficiently
67
68## Guiding Conventions
69
701. Apply "Convention Over Configuration"
712. Enforce security throughout all layers
723. Maintain modular architecture
734. Document comprehensively
745. Extend via inheritance, never modify core code
75
76## Module Structure Best Practices
77
78```
79module_name/
80├── __init__.py
81├── __manifest__.py
82├── models/
83│ ├── __init__.py
84│ └── model_name.py
85├── views/
86│ └── model_name_views.xml
87├── security/
88│ ├── ir.model.access.csv
89│ └── security_rules.xml
90├── data/
91│ └── data.xml
92├── controllers/
93│ ├── __init__.py
94│ └── main.py
95├── static/
96│ └── src/
97├── wizards/
98│ ├── __init__.py
99│ └── wizard_name.py
100└── reports/
101 └── report_templates.xml
102```
103
104## Model Definition Example
105
106```python
107from odoo import models, fields, api
108from odoo.exceptions import ValidationError
109
110class CustomModel(models.Model):
111 _name = 'custom.model'
112 _description = 'Custom Model'
113
114 name = fields.Char(string='Name', required=True)
115 active = fields.Boolean(default=True)
116 state = fields.Selection([
117 ('draft', 'Draft'),
118 ('confirmed', 'Confirmed'),
119 ], default='draft')
120
121 @api.depends('name')
122 def _compute_display_name(self):
123 for record in self:
124 record.display_name = record.name
125
126 @api.constrains('name')
127 def _check_name(self):
128 for record in self:
129 if len(record.name) < 3:
130 raise ValidationError("Name must be at least 3 characters")
131```
132
133## View Definition Example
134
135```xml
136<record id="custom_model_form" model="ir.ui.view">
137 <field name="name">custom.model.form</field>
138 <field name="model">custom.model</field>
139 <field name="arch" type="xml">
140 <form>
141 <header>
142 <field name="state" widget="statusbar"/>
143 </header>
144 <sheet>
145 <group>
146 <field name="name"/>
147 <field name="active"/>
148 </group>
149 </sheet>
150 </form>
151 </field>
152</record>
153```