一键导入
creating-models
Step-by-step guide to create a new Odoo model with fields, constraints, and methods.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Step-by-step guide to create a new Odoo model with fields, constraints, and methods.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Create automated actions (base.automation) and server actions in Odoo.
Create HTTP controllers and API endpoints in Odoo.
Create custom OWL (Odoo Web Library) frontend components.
Create QWeb PDF reports and report actions in Odoo.
Set up access rights (ACLs) and record rules for Odoo models.
Write Python and JavaScript tests for Odoo modules.
| name | Creating Models |
| description | Step-by-step guide to create a new Odoo model with fields, constraints, and methods. |
| globs | ["models/**/*.py","__manifest__.py"] |
Create the Python file in models/ directory (e.g. models/my_model.py).
Define the model class:
from odoo import models, fields, api, _
from odoo.exceptions import ValidationError
class MyModel(models.Model):
_name = 'my.model'
_description = 'My Model'
_order = 'sequence, name'
name = fields.Char(string='Name', required=True)
sequence = fields.Integer(default=10)
active = fields.Boolean(default=True)
state = fields.Selection([
('draft', 'Draft'),
('confirmed', 'Confirmed'),
('done', 'Done'),
], default='draft', string='Status', tracking=True)
partner_id = fields.Many2one('res.partner', string='Partner')
line_ids = fields.One2many('my.model.line', 'model_id', string='Lines')
tag_ids = fields.Many2many('my.model.tag', string='Tags')
total = fields.Float(compute='_compute_total', store=True)
@api.depends('line_ids.amount')
def _compute_total(self):
for record in self:
record.total = sum(record.line_ids.mapped('amount'))
@api.constrains('name')
def _check_name(self):
for record in self:
if record.name and len(record.name) < 3:
raise ValidationError(_("Name must be at least 3 characters."))
def action_confirm(self):
self.write({'state': 'confirmed'})
Register in __init__.py: Add from . import my_model in models/__init__.py.
Add to manifest: Ensure models/ is imported in the module's root __init__.py.
Add security: Create ACL in security/ir.model.access.csv.
Create views: Add form and list views in views/my_model_views.xml.
_name and _description set@api.depends@api.constrains__init__.py chainir.model.access.csv__manifest__.py data list