
Using scaffold to create a module
When creating a new Odoo module, there is some boilerplate that needs to be set up. To help quick starting new modules, Odoo provides the scaffold
command.
The recipe shows how to create a new module using the scaffold
command, which will put in place a skeleton of the files directories to use.
Getting ready
We need Odoo installed and a directory for our custom modules.
We will assume that Odoo is installed at ~/odoo-dev/odoo
and our custom modules will be at ~/odoo-dev/local-addons
.
How to do it…
The scaffold
command is used from the command line:
- Change the working directory to where we will want our module to be. This can be whatever directory you choose, but within an addons path to be useful. Following the directory choices used in the previous recipe, it should be as follows:
$ cd ~/odoo-dev/local-addons
- Choose a technical name for the new module and use the
scaffold
command to create it. For our example, we will choosemy_scaffolded
:$ ~/odoo-dev/odoo/odoo.py scaffold my_scaffolded
- Edit the
__openerp__.py
default module manifest provided and change the relevant values. You will surely want to at least change the module title in thename
key.
This is how the generated addon module should look like:
$ tree my_scaffolded my_scaffolded ├── controllers.py ├── demo.xml ├── __init__.py ├── models.py ├── __openerp__.py ├── security │ └── ir.model.access.csv └── templates.xml
How it works…
The scaffold
command creates the skeleton for a new module based on a template.
By default, the new module is created in the current working directory, but we can provide a specific directory where to create the module, passing it as an additional parameter. For example:
$ ~/odoo-dev/odoo/odoo.py scaffold my_module ~/odoo-dev/local-addons
A default
template is used but a theme
template is also available, for website theme authoring. To choose a specific template, the -t
option can be used. We are also allowed to use a path for a directory with a template.
This means that we can use our own templates with the scaffold
command. The built-in themes can be used as a guide, and they can be found in the Odoo subdirectory ./openerp/cli/templates
. To use our own template, we could use something like this:
$ ~/odoo-dev/odoo/odoo.py scaffold -t path/to/template my_module
There's more…
Unfortunately, the default template does not adhere to the current Odoo guidelines. We could create our own template by copying the default one, at odoo/openerp/cli/templates/default/
, and modifying to better suit the structure described in the Organizing the module file structure recipe. A command similar to this could get us started on that:
$ cp -r ~/odoo-dev/odoo/openerp/cli/templates/default ~/ odoo-dev/template
Later, we can use it with the following command:
$ ~/odoo-dev/odoo/odoo.py scaffold -t ~/odoo-dev/template my_module