Guide To Odoo CLI Commands

As every application , Odoo also comes with a set of cli commands that can be used to run and configure the Odoo. This is more useful for the developers. For this we have to clone any version of Odoo from git, i will be using the Odoo 9 as an example.

There are mainly 3 script files in the Odoo which are as follows

                    // for the python scripts
openerp-server // bash scripts for odoo
openerp-gevent // bash script for multiprocessing

All of this file eventually run the main class of the Odoo openerp.cli.main() and then start the openerp.service.server.start which is the Odoo service using the configuration that we provided, if no configuration provided Odoo will run with the default configuration.

CLI Commands

We can start the Odoo server with simply typing the python using the default configuration. We can supply additional flags to run Odoo as like the xmlrpc port and addons paths etc. To get all the possible flags for the Odoo cli we can run the command like python --help it will list all the possible flags that we can pass with the . The list of all the flags can be found here.

Following are the most useful cli commands. Important thing to note here is Odoo loads the configuration in 3 steps. First loading the default configuration and second from the configuration file then last from the cli configuration that we supplied each time it will overwrite the previous one.

// Some use full commands
python -d odoodb -i sale // will install sale in in the db odoodb, we can supply more modules as comma separated values.
python -d odoodb -u sale //will upgrade sale in in the db odoodb, we can supply more modules as comma separated values.
//Note: before upgrading all modules, safer to check correct dependency is applied to all modules
python -d odoodb -u all // upgrade all modules
python -s -c odoo-config.cfg --stop-after-init // save the current config to a file
python -c odoo-config.cfg // run the odoo with the configuration from a file
python -c odoo-config.cfg --db-filter odoodb // run the odoo with the dbfilter

--stop-after-init tells Odoo to stop server after initialization. The main difference between the Odoo config file and the cli flags is missing hyphens in beginning and replacing all the hyphens in the middle with underscore for config file. Another interesting feature Odoo CLI is you can create a skeleton frame of your new module with sample codes. This is called scaffolding.

// Odoo Scaffold Command
python scaffold module_name // Will create a skeleton frame of your new module.
python scaffold my_module /path //will create in the specified path.
python scaffold -t path/to/template my_module //Create it using the template provided

Odoo Shell Commands

As we do the test using the Odoo gui we can also do the same thing with the Odoo shell such as calling a function from the model etc. This is mostly for developers.

//Odoo Shell Commands
python shell -c odoo-config.cfg --log-level=error // start the Odoo shell with showing only errors in the log

After running it, we will be inside a python command line prompt. In this we have access to certain Odoo keywords such as env and openerp we can use the env for access any model from Odoo and execute any function behalf of it.

//Odoo Shell Commands env
sale = env['sale.order'] // creating an object for sale order
sale = env.ref('sale.view_order_form') // Getting the records using the xml_id //committing to the database //rollback to previous db state

Important point to note is that when you are running in Odoo shell we have to commit the changes to the database using the database cursor, because in the Odoo GUI the database commits are handled by the Odoo itself.

Extra Debugging Methods By Python

There is a useful package in the python which is the ipdb which can be used to find out what happens inside a function in the Odoo step wise. ipdb stands for python debugger.

//Debugging with ipdb
sudo pip install ipdb // pip installing the ipdb
// Add Trace to the begining of any func that you want to debug
import ipdb; ipdb.set_trace() // importing and starting the trace

When python enters the function it will give you the ipdb console in the log with specifying the current execution line. You can list all the variables that you are using in the function by just typing the name. There is also wide variety of commands that you can use with the ipdb such as n for next line and c for continuing the execution. ipdb is a nice tool for debugging python program's in real time.

The best way to understand is to practice every debugging methods that i explained here, so next time when an error occurs you know how to solve it.