Code Snippet Reference
This section provides a quick and practical reference to commonly used code snippets in Odoo development. From defining models in Python to creating views in XML, these examples are designed to help you write code efficiently and consistently. Ideal for both beginners and experienced developers, this guide serves as a go-to resource for everyday development tasks.
Python Keywords
🔧 Fields
• odoo_field_boolean
: Create Boolean field
fields.Boolean(string='Active')
• odoo_field_char
: Create Char field
fields.Char(string='Name')
• odoo_field_text
: Create Text field
fields.Text(string='Description')
• odoo_field_float
: Create Float field
fields.Float(string='Amount', digits=(16, 2))
• odoo_field_integer
: Create Integer field
fields.Integer(string='Quantity')
• odoo_field_selection
: Create Selection field
fields.Selection([
('draft', 'Draft'),
('done', 'Done')
], string='Status', default='draft')
• odoo_field_date
: Create Date field
fields.Date(string='Start Date')
• odoo_field_datetime
: Create Datetime field
fields.Datetime(string='Timestamp')
• odoo_field_many2one
: Create Many2one field
fields.Many2one('model.name', string='Partner', ondelete='restrict')
• odoo_field_one2many
: Create One2many field
fields.One2many('model.name', 'inverse_field_name', string='Order Lines')
• odoo_field_many2many
: Create Many2many field
fields.Many2many('model.name', string='Tags')
• odoo_field_html
: Create HTML field
fields.Html(sanitize=True, string='Content')
• odoo_field_binary
: Create Binary field
fields.Binary(attachment=True, string='File')
🔧 API Decorators
• odoo_api_create_multi
: Add model_create_multi decorator
@api.model_create_multi
• odoo_api_depends
: Add depends decorator
@api.depends('field_name')
• odoo_api_onchange
: Add onchange decorator
@api.onchange('field_name')
• odoo_api_constrains
: Add constrains decorator
@api.constrains('field_name')
• odoo_api_returns
: Add returns decorator
@api.returns('model.name')
🔧 Imports
• odoo_import_fields
: Import fields
from odoo import fields
• odoo_import_models
: Import models
from odoo import models
• odoo_import_api
: Import API
from odoo import api
• odoo_import_tools
: Import tools
from odoo import tools
• odoo_import_config
: Import config
from odoo.tools import config
• odoo_import_date_utils
: Import date_utils
from odoo.tools import date_utils
• odoo_import_common
: Import common modules
from odoo import api, fields, models
🔧 Exceptions
• odoo_import_exceptions
: Import exceptions
from odoo.exceptions import UserError, ValidationError, AccessError, MissingError
• odoo_user_error
: Raise UserError
raise UserError(_('Error message'))
• odoo_validation_error
: Raise ValidationError
raise ValidationError(_('Validation error message'))
• odoo_access_error
: Raise AccessError
raise AccessError(_('Access error message'))
• odoo_missing_error
: Raise MissingError
raise MissingError(_('Record not found'))
• odoo_redirect_warning
: Raise RedirectWarning
raise RedirectWarning(_('Warning message'), action_id, _('Button text'))
🔧 Model/Class Related
• odoo_class
: Create new Odoo Model class
class ModelName(models.Model):
_name = 'model.name'
_description = 'New Model Description'
_order = 'id desc' # Default ordering
name = fields.Char()
• odoo_transient_class
: Create TransientModel class
class ModelName(models.TransientModel):
_name = 'model.name'
_description = 'Wizard Description'
name = fields.Char()
• odoo_abstract_class
: Create AbstractModel class
class ModelName(models.AbstractModel):
_name = 'model.name'
_description = 'Abstract Model Description'
name = fields.Char()
• odoo_extension_inheritance
: Create extension inheritance
class ModelName(models.Model):
_name = 'model.name'
_inherit = 'model.to.inherit'
name = fields.Char()
• odoo_classical_inheritance
: Create classical inheritance
class ModelName(models.Model):
_inherit = 'model.name'
new_field = fields.Char()
• odoo_delegation_inheritance
: Create delegation inheritance
class ModelName(models.Model):
_name = 'model.name'
_inherits = {'res.partner': 'partner_id'}
partner_id = fields.Many2one('res.partner', required=True, ondelete='cascade')
🔧 Methods
• odoo_create_method
: Create standard create method
@api.model_create_multi
def create(self, vals):
"""Create a new record with the given values."""
return super().create(vals)
• odoo_write_method
: Create standard write method
def write(self, vals):
"""Update the record with the given values."""
return super().write(vals)
• odoo_unlink_method
: Create standard unlink method
def unlink(self):
"""Delete the current record."""
return super().unlink()
• odoo_computed_field
: Create computed field with method
computed_field = fields.Float(string='Computed Field', compute='_compute_computed_field', store=True)
@api.depends('dependency_field')
def _compute_computed_field(self):
"""Compute the value of the field computed_field."""
for record in self:
record.computed_field = 0.0 # Your computation here
• odoo_onchange_method
: Create onchange method
@api.onchange('field_name')
def _onchange_field_name(self):
"""Triggered when the field_name changes to update related values."""
if self.field_name:
# Your onchange logic here
self.another_field = self.field_name
🔧 Actions and Notifications
• odoo_return_action
: Create return action
return {
'type': 'ir.actions.act_window',
'name': 'Window Title',
'res_model': 'model.name',
'view_mode': 'list,form',
'target': 'current/new',
'domain': [('field_name', '>', value)],
'context': {'default_field_name': field_value},
}
• odoo_rainbow_man_notification
: Create rainbow man notification
return {
'effect': {
'fadeout': 'slow',
'message': f'Message to Display',
'type': 'rainbow_man',
}
}
• odoo_warning_notification
: Create warning notification
return {
'type': 'ir.actions.client',
'tag': 'display_notification',
'params': {
'title': _("Warning head"),
'type': 'warning',
'message': _("This is the detailed warning"),
'sticky': True,
},
}
• odoo_success_notification
: Create success notification
return {
'type': 'ir.actions.client',
'tag': 'display_notification',
'params': {
'title': _("Warning head"),
'type': 'success',
'message': _("This is the detailed warning"),
'sticky': True,
},
}
• odoo_info_notification
: Create info notification
return {
'type': 'ir.actions.client',
'tag': 'display_notification',
'params': {
'title': _("Warning head"),
'type': 'info',
'message': _("This is the detailed warning"),
'sticky': True,
},
}
XML Keywords
🔧 Buttons and Actions
• odoo_btn_object
: Add Object Button
<button name="button_method_name" type="object" string="Button Label" class="btn-primary"/>
• odoo_btn_action
: Add Action Button
<button name="%(action_id)d" type="action" string="Action Button" class="btn-secondary"/>
• odoo_button_smart
: Add Smart Button
<button name="Button Action"
class="oe_stat_button"
icon="fa-bars"
type="object"
invisible=""
groups="">
<div class="o_stat_info">
<field name="" class="o_stat_value"/>
<span class="o_stat_text"/>
</div>
</button>
• odoo_button_box
: Add Button Box
<div class="oe_button_box" name="button_box">
<button name="Button Action"
class="oe_stat_button"
icon="fa-bars"
type="object"
invisible=""
groups="">
<div class="o_stat_info">
<field name="" class="o_stat_value"/>
<span class="o_stat_text"></span>
</div>
</button>
</div>
🔧 Field Attributes
• odoo_domain
: Add Domain to Field
domain="[('field_name', '=', value)]"
• odoo_options
: Add Options to Field
options="{'currency_field': 'currency_id'}"
• odoo_groups
: Add Groups Restriction
groups="base.group_user"
• odoo_field_widget
: Add Widget in Fields
widget=""
• odoo_field_invisible
: Add Invisible Condition
invisible="state != 'done'"
• odoo_field_readonly
: Add Readonly Condition
readonly="not can_edit"
• odoo_field_required
: Add Required Condition
required="action == 'exist'"
🔧 View Elements
• odoo_field
: Add Field in Form/View
<field name="field_name" string="Field Label" required="True"/>
• odoo_label
: Add Label
<label for="field_name" string="Label Text"/>
• odoo_separator
: Add Separator
<separator string="Section Title"/>
• odoo_chatter
: Add Chatter In Form
<chatter/>
• odoo_notebook
: Add Notebook Block
<notebook>
<page string="New Page">
<!-- Page content goes here -->
</page>
</notebook>
• odoo_xpath
: Add Xpath Block
<xpath expr="//div" position="inside">
<field name="name"/>
</xpath>
• odoo_page
: Add Page Element
<page string="">
<!-- Page content goes here -->
</page>
• odoo_header
: Add Header with Button + Statusbar
<header>
<button name="" string="" class="oe_highlight" states="" type=""/>
<field name="state" widget="statusbar" statusbar_visible="" statusbar_colors="{'draft':'blue','done':'green'}"/>
</header>
• odoo_sheet
: Add Sheet with Group and Field
<sheet>
<group>
<field name="name"/>
<field name="description"/>
</group>
</sheet>
• odoo_record
: Add Record Tag
<record id="" model="">
</record>
🔧 View Templates
• odoo_form_view
: Create Form View Template
<record id="model_name_view_form" model="ir.ui.view">
<field name="name">model.name.view.form</field>
<field name="model">model.name</field>
<field name="arch" type="xml">
<form string="Form Title">
<header>
<!-- Buttons and statusbar go here -->
</header>
<sheet>
<div class="oe_title">
<h1>
<field name="name"/>
</h1>
</div>
<group>
<group>
<field name="field1"/>
<field name="field2"/>
</group>
<group>
<field name="field3"/>
<field name="field4"/>
</group>
</group>
<notebook>
<field name="line_ids">
<field name="line_field1"/>
<field name="line_field2"/>
</field>
</notebook>
</sheet>
</form>
</field>
</record>
• odoo_list_view
: Create List View Template
<record id="model_name_view_list" model="ir.ui.view">
<field name="name">model.name.view.list</field>
<field name="model">model.name</field>
<field name="arch" type="xml">
<list string="List Title" decoration-danger="state=='cancel'" decoration-success="state=='done'">
<field name="name"/>
<field name="date"/>
<field name="partner_id"/>
<field name="amount"/>
<field name="state"/>
</list>
</field>
</record>
• odoo_search_view
: Create Search View Template
<record id="model_name_view_search" model="ir.ui.view">
<field name="name">model.name.view.search</field>
<field name="model">model.name</field>
<field name="arch" type="xml">
<search string="Search Title">
<field name="name" string="Name" filter_domain="[('name','ilike',self)]"/>
<field name="partner_id"/>
<field name="date"/>
<filter string="Draft" name="draft" domain="[('state','=','draft')]"/>
<filter string="Done" name="done" domain="[('state','=','done')]"/>
<separator/>
<filter string="My Records" name="my_records" domain="[('user_id','=',uid)]"/>
<group expand="0" string="Group By">
<filter string="Partner" name="partner" context="{'group_by':'partner_id'}"/>
<filter string="Month" name="month" context="{'group_by':'date:month'}"/>
<filter string="Status" name="status" context="{'group_by':'state'}"/>
</group>
</search>
</field>
</record>
• odoo_kanban
: Add Kanban View Template
<kanban class="o_kanban_example">
<field name="name"/>
<templates>
<t t-name="kanban-box">
<div class="oe_kanban_global_click">
<strong><field name="name"/></strong>
<div><field name="description"/></div>
</div>
</t>
</templates>
</kanban>
• odoo_pivot_view
: Create Pivot View
<record id="model_name_view_pivot" model="ir.ui.view">
<field name="name">model.name.view.pivot</field>
<field name="model">model.name</field>
<field name="arch" type="xml">
<pivot string="Model Name Pivot">
<field name="name"/>
<field name="state"/>
</pivot>
</field>
</record>
• odoo_calender_view
: Create Calendar View
<record id="model_name_view_calender" model="ir.ui.view">
<field name="name">model.name.view.calender</field>
<field name="model">model.name</field>
<field name="arch" type="xml">
<calender string="Model Name Calender">
<field name="name"/>
<field name="state"/>
</calender>
</field>
</record>
🔧 Menu Items
• odoo_menu_item_root
: Add Root Menu Item
<menuitem id="module_name_menu_root" name="Module Name" sequence="10"/>
• odoo_menu_item_categ
: Add Category Menu Item
<menuitem id="unique_id_menu_categ" name="Model Name" parent="" sequence="10"/>
• odoo_menu_item_action
: Add Action Menu Item
<menuitem id="unique_id_menu_categ" name="Model Name" parent="" action="" sequence="10"/>
🔧 Action Windows
• odoo_action
: Create Action Window
<record id="model_name_action" model="ir.actions.act_window">
<field name="name">Model Name</field>
<field name="res_model">model.name</field>
<field name="view_mode">list,form</field>
<field name="context">{}</field>
<field name="help" type="html">
<p class="o_view_nocontent_smiling_face">
Create your first record
</p>
</field>
</record>
• odoo_inherit_view
: Create Inherit View
<record id="model_name_view_type_view_inherit" model="ir.ui.view">
<field name="name">model.name.view.type.view.inherit</field>
<field name="model">model.name</field>
<field name="inherit_id" ref="module.original_form_view_id"/>
<field name="arch" type="xml">
<field name="arch" type="xml">
<!-- Add fields to an existing group -->
<xpath expr="//group[1]" position="inside">
<field name="new_field1"/>
<field name="new_field2"/>
</xpath>
</field>
</record>
• odoo_tag
: Add Odoo Tag
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
</odoo>