Odoo Guidelines
Odoo Guidelines
Odoo Guidelines
Navigate
Odoo Guidelines
This page introduces the new Odoo Coding Guidelines. Those aim to improve the quality of the code (e.g.
better readability of source) and Odoo Apps. Indeed, proper code eases maintenance, aids debugging, lowers
complexity and promotes reliability.
These guidelines should be applied to every new module, and new developpment. These guidelines will be
applied to old module only in case of code refactoring (migration to new API, big refactoring, ...).
Warning
These guidelines are written with new modules and new files in mind. When modifying existing files,
the original style of the file strictly supersedes any other style guidelines. In other words, never
modify existing files in order to apply these guidelines, to avoid disrupting the revision history of
each line. For more details, see our pull request guide.
Module structure
Directories
A module is organised in important directories. Those contain the business logic; having a look at them should
make understand the purpose of the module.
wizard/ : regroups the transient models (formerly osv_memory) and their views.
report/ : contains the reports (RML report [deprecated], models based on SQL views (for reporting) and
other complex reports). Python objects and XML views are included in this directory.
tests/ : contains the Python/YML tests
File naming
For views declarations, split backend views from (frontend) templates in 2 differents files.
For models, split the business logic by sets of models, in each set select a main model, this model gives its
name to the set. If there is only one model, its name is the same as the module name. For each set named
<main_model> the following files may be created:
models/<main_model>.py
models/<inherited_main_model>.py
views/<main_model>_templates.xml
views/<main_model>_views.xml
For instance, sale module introduces sale_order and sale_order_line where sale_order is dominant. So the
<main_model> files will be named models/sale_order.py and views/sale_order_views.py .
For data, split them by purpose : demo or data. The filename will be the main_model name, suffixed by
_demo.xml or _data.xml.
For controllers, the only file should be named main.py. Otherwise, if you need to inherit an existing controller
from another module, its name will be <module_name>.py. Unlike models, each controller class should be
contained in a separated file.
For static files, since the resources can be used in different contexts (frontend, backend, both), they will be
included in only one bundle. So, CSS/Less, JavaScript and XML files should be suffixed with the name of the
bundle type. i.e.: im_chat_common.css, im_chat_common.js for 'assets_common' bundle, and
im_chat_backend.css, im_chat_backend.js for 'assets_backend' bundle. If the module owns only one file, the
convention will be <module_name>.ext (i.e.: project.js). Don't link data (image, libraries) outside Odoo: do not
use an URL to an image but copy it in our codebase instead.
Regarding data, split them by purpose: data or demo. The filename will be the main_model name, suffixed by
_data.xml or _demo.xml.
<main_transient>.py
<main_transient>_views.xml
Where <main_transient> is the name of the dominant transient model, just like for models.
<main_transient>.py can contains the models 'model.action' and 'model.action.line'.
<report_name_A>_report.py
<report_name_A>_report_views.py (often pivot and graph views)
Note
File names should only contain [a-z0-9_] (lowercase alphanumerics and _ )
Warning
Use correct file permissions : folder 755 and file 644.
XML les
Format
To declare a record in XML, the record notation (using <record>) is recommended:
Naming xml_id
Security, View and Action
Use the following pattern :
<menuitem
id="model_name_menu_root"
name="Main Menu"
sequence="5"
/>
<menuitem
id="model_name_menu_action"
name="Sub Menu 1"
parent="module_name.module_name_menu_root"
action="model_name_action"
sequence="10"
/>
Note
View names use dot notation my.model.view_type or my.model.view_type.inherit instead of "This is the
form view of My Model".
Inherited XML
The naming pattern of inherited view is <base_view>_inherit_<current_module_name> . A module may only extend
a view once. Suffix the orginal name with _inherit_<current_module_name> where current_module_name is the
technical name of the module extending the view.
Python
PEP8 options
Using a linter can help show syntax and semantic warnings or errors. Odoo source code tries to respect Python
standard, but some of them can be ignored.
Imports
The imports are ordered as
1. External libraries (one per line sorted and split in python stdlib)
2. Imports of odoo
3. Imports from Odoo modules (rarely, and only if necessary)
# bad
new_dict = my_dict.clone()
new_list = old_list.clone()
# good
new_dict = dict(my_dict)
new_list = list(old_list)
# -- update dict
# bad
my_dict['foo'] = 3
my_dict['bar'] = 4
my_dict['baz'] = 5
# good
my_dict.update(foo=3, bar=4, baz=5)
my_dict = dict(my_dict, **my_dict2)
# clearer
def axes(self, axis):
if type(axis) == type([]):
return list(axis) # clone the axis
else:
return [axis] # single-element list
Know your builtins : You should at least have a basic understanding of all the Python builtins
(http://docs.python.org/library/functions.html)
Also, if 'key' in my_dict and if my_dict.get('key') have very different meaning, be sure that you're using the right
one.
Learn list comprehensions : Use list comprehension, dict comprehension, and basic manipulation using
map , filter , sum , ... They make the code easier to read.
Collections are booleans too : In python, many objects have "boolean-ish" value when evaluated in a
boolean context (such as an if). Among these are collections (lists, dicts, sets, ...) which are "falsy" when
empty and "truthy" when containing items:
bool([]) is False
bool([1]) is True
bool([False]) is True
Iterate on iterables
Use dict.setdefault
# longer.. harder to read
values = {}
for element in iterable:
if element not in values:
values[element] = []
values[element].append(other_value)
As a good developper, document your code (docstring on methods, simple comments for tricky part of
code)
In additions to these guidelines, you may also find the following link interesting:
http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html (a little bit outdated, but quite
relevant)
Programming in Odoo
Avoid to create generators and decorators: only use the ones provided by the Odoo API.
As in python, use filtered , mapped , sorted , ... methods to ease code reading and performance.
@api.multi
def my_method(self)
for record in self:
record.do_cool_stuff()
Avoid to use api.one decorator : this will probably not do what you expected, and extending a such method is
not as easy than a api.multi method, since it returns a list of result (ordered by recordset ids).
For performance issue, when developping a 'stat button' (for instance), do not perform a search or a
search_count in a loop in a api.multi method. It is recommended to use read_group method, to compute all
value in only one request.
@api.multi
def _compute_equipment_count(self):
""" Count the number of equipement per category """
equipment_data = self.env['hr.equipment'].read_group([('category_id', 'in', self.ids)], ['category_id'], ['category_id'])
mapped_data = dict([(m['category_id'][0], m['category_id_count']) for m in equipment_data])
for category in self:
category.equipment_count = mapped_data.get(category.id, 0)
Passing parameter in context can have dangerous side-effects. Since the values are propagated automatically,
some behavior can appears. Calling create() method of a model with default_my_field key in context will set
the default value of my_field for the concerned model. But if curing this creation, other object (such as
sale.order.line, on sale.order creation) having a field name my_field, their default value will be set too.
If you need to create a key context influencing the behavior of some object, choice a good name, and
eventually prefix it by the name of the module to isolate its impact. A good example are the keys of mail
module : mail_create_nosubscribe, mail_notrack, mail_notify_user_signature, ...
# better
auction_lots_ids = self.search([('auction_id','in',ids), ('state','=','draft'), ('obj_price','>',0)])
The best way to be safe is to never, NEVER use Python string concatenation (+) or string parameters
interpolation (%) to pass variables to a SQL query string.
The second reason, which is almost as important, is that it is the job of the database abstraction layer
(psycopg2) to decide how to format query parameters, not your job! For example psycopg2 knows that when
you pass a list of values it needs to format them as a comma-separated list, enclosed in parentheses !
# better
self.env.cr.execute('SELECT DISTINCT child_id '\
'FROM account_account_consol_rel '\
'WHERE parent_id IN %s',
(tuple(ids),))
This is very important, so please be careful also when refactoring, and most importantly do not copy these
patterns!
Here is a memorable example to help you remember what the issue is about (but do not copy the code there).
Before continuing, please be sure to read the online documentation of pyscopg2 to learn of to use it properly:
Also, name your functions accordingly: small and properly named functions are the starting point of
readable/maintainable code and tighter documentation.
This recommendation is also relevant for classes, files, modules and packages. (See also
http://en.wikipedia.org/wiki/Cyclomatic_complexity)
If any error occurs during the execution of the RPC call, the transaction is rolled back atomically, preserving the
state of the system.
Similarly, the system also provides a dedicated transaction during the execution of tests suites, so it can be
rolled back or not depending on the server startup options.
The consequence is that if you manually call cr.commit() anywhere there is a very high chance that you will
break the system in various ways, because you will cause partial commits, and thus partial and unclean
rollbacks, causing among others:
And by the way if you did create your own cursor, then you need to handle error cases and proper
rollback, as well as properly close the cursor when you're done with it.
And contrary to popular belief, you do not even need to call cr.commit() in the following situations: - in the
_auto_init() method of an models.Model object: this is taken care of by the addons initialization method, or by
the ORM transaction when creating custom models - in reports: the commit() is handled by the framework too,
so you can update the database even from within a report - within models.Transient methods: these methods
are called exactly like regular models.Model ones, within a transaction and with the corresponding
cr.commit()/rollback() at the end - etc. (see general rule above if you have in doubt!)
All cr.commit() calls outside of the server framework from now on must have an explicit comment explaining
why they are absolutely necessary, why they are indeed correct, and why they do not break the transactions.
Otherwise they can and will be removed !
A few very important rules must be followed when using it, in order for it to work and to avoid filling the
translations with useless junk.
Basically, this method should only be used for static strings written manually in the code, it will not work to
translate field values, such as Product names, etc. This must be done instead using the translate flag on the
corresponding field.
The rule is very simple: calls to the underscore method should always be in the form _('literal string') and
nothing else:
# good: plain strings
error = _('This record is locked!')
Also, keep in mind that translators will have to work with the literal values that are passed to the underscore
function, so please try to make them easy to understand and keep spurious characters and formatting to a
minimum. Translators must be aware that formatting patterns such as %s or %d, newlines, etc. need to be
preserved, but it's important to use these in a sensible and obvious manner:
In general in Odoo, when manipulating strings, prefer % over .format() (when only one variable to replace in a
string), and prefer %(varname) instead of position (when multiple variables have to be replaced). This makes
the translation easier for the community translators.
Odoo Python Class : use camelcase for code in api v8 (Object-oriented style), underscore lowercase
notation for old api (SQL style).
class AccountInvoice(models.Model):
...
class account_invoice(osv.osv):
...
Variable name :
use camelcase for model variable
use underscore lowercase notation for common variable.
since new API works with record or recordset instead of id list, don't suffix variable name with
_id or _ids if they not contain id or list of id.
ResPartner = self.env['res.partner']
partners = ResPartner.browse(ids)
partner_id = partners[0].id
One2Many and Many2Many fields should always have _ids as suffix (example: sale_order_line_ids)
Many2One fields should have _id as suffix (example : partner_id, user_id, ...)
Method conventions
Compute Field : the compute method pattern is _compute_<field_name>
Search method : the search method pattern is _search_<field_name>
Default method : the default method pattern is _default_<field_name>
Onchange method : the onchange method pattern is _onchange_<field_name>
Constraint method : the constraint method pattern is _check_<constraint_name>
Action method : an object action method is prefix with action_. Its decorator is @api.multi , but
since it use only one record, add self.ensure_one() at the beginning of the method.
# Default methods
def _default_name(self):
...
# Fields declaration
name = fields.Char(string='Name', default=_default_name)
seats_reserved = fields.Integer(oldname='register_current', string='Reserved Seats',
store=True, readonly=True, compute='_compute_seats')
seats_available = fields.Integer(oldname='register_avail', string='Available Seats',
store=True, readonly=True, compute='_compute_seats')
price = fields.Integer(string='Price')
@api.onchange('date_begin')
def _onchange_date_begin(self):
...
# Action methods
@api.multi
def action_validate(self):
self.ensure_one()
...
# Business methods
def mail_user_confirm(self):
...
odoo.website.if_dom_contains('.jquery_class_selector', function () {
/*your code here*/
});
For CSS :
Prefix all your classes with o_<module_name> where module_name is the technical name of the module
('sale', 'im_chat', ...) or the main route reserved by the module (for website module mainly, i.e. : 'o_forum'
for website_forum module). The only exception for this rule is the webclient: it simply uses o_ prefix.
Avoid using id
Use Bootstrap native classes
Use underscore lowercase notation to name class
Git
Git
Commit message
Prefix your commit with
Then, in the message itself, specify the part of the code impacted by your changes (module name, lib,
transversal object, ...) and a description of the changes.
Always include a meaningful commit message: it should be self explanatory (long enough) including the
name of the module that has been changed and the reason behind the change. Do not use single words
like "bugfix" or "improvements".
Avoid commits which simultaneously impact multiple modules. Try to split into different commits where
impacted modules are different (It will be helpful if we need to revert a module separately).
[FIX] website, website_mail: remove unused alert div, fixes look of input-group-btn
This commit introduces a new module system for the javascript code.
Instead of using global ...
Note
Use the long description to explain the why not the what, the what can be seen in the diff
Community Services
Github Editions
Download Cloud Pricing
Upgrade
Runbot
Translations Find a partner
Become a partner
Mailing Lists
Forum Education
Security
About us
Our company
Contact
Events
Blog
Customers
Jobs
Odoo is a suite of open source business apps that cover all your company needs: CRM, eCommerce, accounting, inventory, point of sale, project
management, etc.
Odoo's unique value proposition is to be at the same time very easy to use and fully integrated.