Skip to content

Snippets ​

Insert Odoo models, fields, methods, views, actions and menus by typing odoo and choosing a snippet. Snippets follow the Odoo version of your project (18, 19 or 20).

What you can do ​

All snippet prefixes start with odoo. Keep typing to narrow the list, for example odoo form view or odoo char field. The suggestion list shows a short description, and many snippets show documentation next to it. Press Tab to move between placeholders.

The tables below mark the versions that offer each snippet. A snippet that only one version offers does not appear on the others.

Python models ​

PrefixInserts181920
odoo new model classA model with _name, _description, _order, name and active fields, and a unique-name constraint.YesYesYes
odoo classical inherited model classA new model that copies another one (_name and _inherit, own table).YesYesYes
odoo delegated inherited model classA new model that embeds a parent record (_inherits).YesYesYes
odoo extended inherited model classAn extension of an existing model (_inherit, same table).YesYesYes
odoo abstract modelAn abstract model (mixin) with no table.YesYesYes
odoo transient modelA wizard model with a confirm button method.YesYesYes
odoo sql constraints_sql_constraints.Yes
odoo constraintmodels.Constraint, which replaces _sql_constraints.YesYes
odoo indexA database index (models.Index).Yes
odoo unique indexA unique, optionally partial, index with an error message (models.UniqueIndex).Yes
odoo controllerAn HTTP controller with a page route and a JSON-RPC route.Yes

Python fields ​

These are available in all versions. Choose them with odoo <type> field.

PrefixInserts
odoo boolean fieldA true or false value.
odoo char fieldShort text, optionally translatable.
odoo text fieldLong plain text.
odoo html fieldRich text, sanitized by default.
odoo integer fieldA whole number.
odoo float fieldA decimal number, with a precision.
odoo monetary fieldAn amount in a currency. The model needs the currency field it names, such as currency_id.
odoo date fieldA date, with no time.
odoo datetime fieldA date and time, stored in UTC.
odoo selection fieldA choice from a fixed list of value and label pairs.
odoo many2one fieldA link to one record of another model, with ondelete.
odoo one2many fieldThe records of another model that point back through their Many2one.
odoo many2many fieldLinks to any number of records of another model.
odoo binary fieldFile content, stored as an attachment.
odoo image fieldAn image resized to fit max_width and max_height.
odoo reference fieldA link to a record of one of several models.
odoo json fieldA JSON value.
odoo many2oneReference fieldA record ID whose model comes from a Char field (Odoo 20 only).

The field classes and parameters you get when you type fields. and = also follow your version. See Smart suggestions.

Python methods ​

PrefixInserts181920
odoo create methodAn override of create with @api.model_create_multi.YesYesYes
odoo write methodAn override of write.YesYesYes
odoo unlink methodAn override of unlink.YesYesYes
odoo onchange methodAn @api.onchange method.YesYesYes
odoo compute methodA computed field with its @api.depends method.YesYesYes
odoo constraints methodAn @api.constrains method that validates records.YesYesYes
odoo ondelete methodAn @api.ondelete method that blocks deletion under a condition.Yes
odoo search name methodA _search_display_name method that makes name search match another field.Yes
odoo display name methodA _compute_display_name method that sets the record label.Yes
odoo default get methodAn override of default_get.Yes
odoo action methodA method that returns a window action, for example from a smart button.Yes

Typing @api. offers each decorator as a ready-made method, for example @api.depends with a def below it. The decorators offered follow your version.

VersionDecorators
18autovacuum, constrains, depends, depends_context, model, model_create_multi, onchange, ondelete, private, readonly, returns
19 and 20The same, without returns

Python errors, notifications and actions ​

These are available in all versions. They use self.env._() for translation.

PrefixInsertsNeeds
odoo validation errorraise ValidationError(...)from odoo.exceptions import ValidationError
odoo user errorraise UserError(...)from odoo.exceptions import UserError
odoo access errorraise AccessError(...)from odoo.exceptions import AccessError
odoo missing errorraise MissingError(...)from odoo.exceptions import MissingError
odoo redirect warningA warning with a redirect button.from odoo.exceptions import RedirectWarning
odoo notificationA display_notification client action with a title, message, type and sticky flag. Return it from a button method.
odoo rainbow manThe rainbow man effect. Return it from a button method.
odoo return actionAn ir.actions.act_window that opens a record, in a dialog with target set to new.
odoo url actionAn ir.actions.act_url that opens a URL.

Python imports ​

Type odoo in a .py file to get these import snippets.

PrefixInserts
odoo common importsfrom odoo import api, fields, models
odoo import fieldsfrom odoo import fields
odoo import modelsfrom odoo import models
odoo import apifrom odoo import api
odoo import toolsfrom odoo import tools
odoo import configfrom odoo.tools import config
odoo import date_utilsfrom odoo.tools import date_utils
odoo import commonfrom odoo import api, fields, models
odoo import httpfrom odoo import http
odoo import http requestfrom odoo.http import request
odoo import loggingimport logging and _logger = logging.getLogger(__name__)
odoo import jsonimport json
odoo import base64import base64
odoo import datetimefrom datetime import datetime, date, timedelta
odoo import htmlfrom html import escape
odoo import reimport re
odoo import werkzeug.utilsfrom werkzeug.utils import redirect
odoo import exceptionsfrom odoo.exceptions import UserError, ValidationError

XML ​

These are available in all versions.

PrefixInserts
odoo xmlAn <odoo> data file.
odoo recordA record of any model.
odoo form viewA form view.
odoo list viewA list view.
odoo pivot viewA pivot view.
odoo calendar viewA calendar view.
odoo graph viewA graph view.
odoo search viewA search view with a filter and a group by.
odoo kanban viewA kanban view.
odoo view inheritAn inherited view.
odoo xpathAn xpath for view inheritance.
odoo actionA window action.
odoo menu rootAn app (root) menu with an icon.
odoo menu categoryA menu with sub-menus.
odoo menu actionA menu that opens an action.
odoo sheetA form sheet.
odoo headerA form header with buttons and a status bar.
odoo notebookA notebook with a page.
odoo pageA notebook page.
odoo chatterThe chatter. The model must inherit mail.thread.
odoo button boxA container for smart buttons.
odoo smart buttonA smart button.
odoo object buttonA button that calls a model method.
odoo action buttonA button that opens an action.
odoo labelA field label.
odoo fieldA field.
odoo field widgetA field with a widget.
odoo field invisibleA field hidden by a condition.
odoo field readonlyA field made read-only by a condition.
odoo field requiredA field required by a condition.
odoo field stateA status bar.
odoo optionsField options.
odoo domainA domain attribute.
odoo groupsA group restriction.

Odoo 20 adds these:

PrefixInserts
odoo editable listAn inline-editable list view.
odoo field optionalAn optional list column.
odoo column invisibleA list column hidden by a condition on the parent.
odoo ribbonA ribbon widget.
odoo settingsA settings app, block and setting inside base.res_config_settings_view_form.
odoo reportA report action.
odoo cronA scheduled action.
odoo accessAn ir.access rule. With no group, it restricts everyone.

Get started ​

  1. Open a .py or .xml file in an Odoo module.
  2. Type odoo and pick a snippet from the suggestions.
  3. Fill the placeholders with Tab.

Examples (Odoo 20 output) ​

python
from odoo import api, fields, models


class ModelName(models.Model):
    _name = 'model.name'
    _description = 'Model Description'
    _order = 'name'

    name = fields.Char(string='Name', required=True)
    active = fields.Boolean(default=True)

    _name_uniq = models.Constraint('unique(name)', 'The name must be unique.')
python
@api.ondelete(at_uninstall=False)
def _unlink_except_done(self):
    for record in self:
        if record.state == 'done':
            raise UserError(self.env._("You cannot delete this record."))
python
return {
    'type': 'ir.actions.client',
    'tag': 'display_notification',
    'params': {
        'title': self.env._("Title"),
        'message': self.env._("Message"),
        'type': 'success',
        'sticky': False,
    },
}
xml
<record id="model_name_view_kanban" model="ir.ui.view">
    <field name="name">model.name.view.kanban</field>
    <field name="model">model.name</field>
    <field name="arch" type="xml">
        <kanban sample="1">
            <templates>
                <t t-name="card">
                    <field name="name" class="fw-bold fs-5"/>
                </t>
            </templates>
        </kanban>
    </field>
</record>
xml
<record id="access_model_name_user" model="ir.access">
    <field name="name">model.name user</field>
    <field name="model_id" ref="model_model_name"/>
    <field name="group_id" ref="base.group_user"/>
    <field name="operation">crud</field>
    <field name="domain">[(1, '=', 1)]</field>
</record>

Constraints by version ​

python
_name_uniq = models.Constraint(
    'unique(name)',
    'The name must be unique.',
)

Odoo 20 also offers odoo index and odoo unique index.

Tips and limits ​

  • Snippets that only one version offers do not appear on the others. For example, odoo sql constraints appears on Odoo 18, and odoo constraint on 19 and 20.
  • If no snippets appear, check the Odoo version in the status bar and that the file is recognised as Python or XML.
  • Some snippets need an import. Type odoo import exceptions to add the exception classes.
  • Insert odoo chatter only in a form view of a model that inherits mail.thread.

Next steps ​

Built by Cybrosys Technologies.