The Odoo web interface is meant for end users, although the developer mode unlocks a number of powerful features. However, testing and debugging through the web interface is not the easiest way to do things, as you need to manually prepare data, navigate in the menus to perform actions, and so on. The Odoo shell is a command-line interface, which you can use to issue calls. This recipe shows how to start the Odoo shell and perform actions such as calling a method inside the shell.
We will reuse the same code as in the previous recipe to produce server logs to help debug methods; this enables the product.product
model to add a new method. We assume that you have an instance with the addon installed available. In the recipe, we expect that you have an Odoo configuration file for this instance called project.conf
.
In order to call the export_stock_level()
method from the Odoo shell, you need to perform the following steps:
$ odoo/odoo.py shell -c project.conf --log-level=error
env: <openerp.api.Environment object at 0x7fbc244e3a90> openerp: <module 'openerp' from '/home/cookbook/odoo/openerp/__init__.pyc'> self: res.users(1,) Python 2.7.9 (default, Mar 1 2015, 12:57:24) [GCC 4.9.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. (Console) >>>
product.product
:>>> product = env['product.product']
>>> location_stock = env.ref('product.stock_location_stock')
export_stock_level()
method:>>> product.export_stock_level(location_stock)
>>> env.cr.commit()
Step 1 uses
odoo.py shell
to start the Odoo shell. All the usual command line arguments are available. We use -c
to specify a project configuration file and --log-level
to reduce the verbosity of the logs. When debugging, you may want to have a logging level of DEBUG
only for some specific addons.
Before providing you with a Python command-line prompt, odoo.py shell
starts an Odoo instance that does not listen on the network and initializes some global variables, which are mentioned in the output:
env
is an environment connected to the database specified on the command line or in the configuration file.openerp
is the openerp
package imported for you. You get access to all the Python modules within that package so you can do what you want.self
is a recordset of res.users
containing a single record for the Odoo super user (Administrator), which is linked to the environment env
.Step 3 and 4 use env
to get an empty recordset and find a record by XML ID. Step 5 calls the method on the product.product
recordset. These operations are identical to what you would use inside a method, with the small difference that we use env
and not self.env
(although we could have both as they are identical). See Chapter 5, Basic Server Side Development, for more information on what is available.
Step 6 commits the database transaction. This is not strictly necessary here because we did not modify any record in the database, but if we had done so and wanted these changes to persist, this is necessary—when you use Odoo through the web interface, each RPC call runs in its own database transaction and Odoo manages these for you. When running in the shell mode, this no longer happens and you have to call env.cr.commit()
or env.cr.rollback()
yourself. Otherwise, when you exit the shell, any transaction in progress is automatically rolled back. When testing, this is fine, but if you use the shell, for example, to script the configuration of an instance, don't forget to commit your work!