This article gives an overview of how we as a developer work with Odoo Command-Line.
In order to explain it, we have already installed the Odoo v9 series GitHub repository and created a custom configuration file (at /opt/odoo9.conf) with minimal configuration.
Odoo 16 is here now, learn more about what new features it brings to this enterprise management software.
Philosophy of Odoo scripts
There are 3 three script file available of command line access.
- odoo.py(python scripts)
- openerp-server(bash scripts)
- openerp-gevent(bash scripts for gevent environment )
All these scripts file finally run:
import openerp openerp.cli.main()
So basically the execution of the scripts call the main method of openerp.cli which is responsible of running the openerp.service.server.start using the the configuration parameter (from openerp.tools.config)
Let’s start with odoo.py
We can get access of Odoo command line interface by running the odoo.py script file located at the parent directory of our Odoo folder.
odoo.py is nothing but python script just like other bash scripts openerp-server and openerp-gevent.
As while running the ./odoo.py i have yet not associated any custom configuration file/parameters,that why the Odoo is running with these default configuration (~/.openerp_serverrc) as below:
{'addons_path': '/opt/odoo9/openerp/addons,/opt/odoo9/addons', 'admin_passwd': 'admin', 'config': None, 'csv_internal_sep': ',', 'data_dir': '/opt/odoo9/.local/share/Odoo', 'db_host': False, 'db_maxconn': 64, 'db_name': False, 'db_password': False, 'db_port': False, 'db_template': 'template1', 'db_user': False, 'dbfilter': '.*', 'debug_mode': False, 'demo': {}, 'dev_mode': False, 'email_from': False, 'geoip_database': '/usr/share/GeoIP/GeoLiteCity.dat', 'import_partial': '', 'init': {}, 'language': None, 'limit_memory_hard': 2684354560, 'limit_memory_soft': 2147483648, 'limit_request': 8192, 'limit_time_cpu': 60, 'limit_time_real': 120, 'list_db': True, 'load_language': None, 'log_db': False, 'log_db_level': 'warning', 'log_handler': [':INFO'], 'log_level': 'info', 'logfile': None, 'logrotate': False, 'longpolling_port': 8072, 'max_cron_threads': 2, 'osv_memory_age_limit': 1.0, 'osv_memory_count_limit': False, 'overwrite_existing_translations': False, 'pg_path': None, 'pidfile': None, 'proxy_mode': False, 'publisher_warranty_url': 'http://services.openerp.com/publisher-warranty/', 'reportgz': False, 'root_path': '/opt/odoo9/openerp', 'save': None, 'server_wide_modules': None, 'smtp_password': False, 'smtp_port': 25, 'smtp_server': 'localhost', 'smtp_ssl': False, 'smtp_user': False, 'stop_after_init': False, 'syslog': False, 'test_commit': False, 'test_enable': False, 'test_file': False, 'test_report_directory': False, 'translate_in': None, 'translate_modules': ['all'], 'translate_out': None, 'unaccent': False, 'update': {}, 'without_demo': False, 'workers': 0, 'xmlrpc': True, 'xmlrpc_interface': '', 'xmlrpc_port': 8069}
Note: Odoo store the configuration data at openerp.tools.config.options in dict format,where keys represent the configuration parameters and values represent value of parameter.
The default configuration is store in openerp_serverrc placed at $HOME/. openerp_serverrc
We can override all above mention default parameters manually from command line.
For getting the list of available parameters just use –help option after ./odoo.py as below:
./odoo.py --h
xmlrpc port & addons path
and for changing/adding the another addons path use –addons-path <directories>
./odoo.py --xmlrpc-port 9069 --addons-path /opt/odoo9/openerp/addons,/opt/odoo9/addons,/opt/odoo9/customs
Module installation using CLI
Odoo CLI also provide the commands -i <modules>, –init <modules> for module installation as.
For installing the sale :
./odoo.py -d odoo9db -i sale
We can also install multiple module at once by placing the module name in comma separated
./odoo.py -d odoo9db -i sale , product
Module updation using CLI
For module updation we can use -u <modules>, –update <modules> as. In case you are planning to upgrade your existing Odoo platform to version 16, make sure to check out Odoo Data Migration services.
For update the sale :
./odoo.py -d odoo9db -u website_sale
Using a custom configuration
Instead of passing the –xmlrpc-port and –addons-path manually each time while
running the Odoo we can save the most common setting in config file and directly associated the configuration file using -c <config>, –config <config> as below:
./odoo.py -c /etc/odoo9.conf
Here is my odoo9.conf file:
addons_path = /opt/odoo9/openerp/addons,/opt/odoo9/addons,/opt/odoo9/custom #add one another addons path('/opt/odoo9/custom') admin_passwd= webkul, #change my admin_passwd to webkul db_user = odoo9 #associate my odoo9 db_user xmlrpc_port = 9069 #change xmlrpc_port to 9069 logfile= /var/log/odoo/odoo9-server.log #add a separate logfile logrotate = True #enabled log-rotation
So here we don(t) need to pass the generic parameter manually each time , in case if it(s) need we can again pass specif parameters as per our requirement
Module testing using CLI
Odoo provide mainly two type of parameters for test driven development.
- –test-enable (for all module tests)
- –test-file(for particular module tests)
For testing all module in a database(odoo9db):
/odoo.py -c /etc/odoo9.conf -d odoo9db --test-enable
Here we have pass our custom config file path using -c and database name using -d.
For testing particular module(sale) in a database(odoo9db):
./odoo.py -c /etc/odoo9.conf -d odoo9db -u sale --test-file /opt/odoo9/addons/sale/tests
Here we have pass our custom config file path using -c and database name using -d and tests file of sale using –test-file .
NOTE : we don’t need to pass –test-enable if you have used –test-file and also don’t forget to use -i/-u while running test of particular module.
Other USEFUL Command
- –stop-after-init
- –db-filter
- -save
–stop-after-init :it(s) a helper parameter which stop the running Odoo server just after prior parameters(like installation,updation,testing) execution .
./odoo.py -d odoo9db -u bus,fleet --stop-after-init # stop the server after bus,fleet module updatation ./odoo.py -d odoo9db -i website_sale --stop-after-init # stop the server after website_sale module updatation ./odoo.py -c /etc/odoo9.conf -d odoo9db --test-enable --stop-after-init # stop the server after running the tests cases of Odoo modules ./odoo.py -c /etc/odoo9.conf -d odoo9db --test-file /opt/odoo9/addons/sale/tests --stop-after-init # stop the server after running the tests of sale module
–db-filter :it parameter which can use for hides databases that do not match regular expression .
./odoo.py -c /etc/odoo9.conf --db-filter webkul.* # filter the database name start with webkul like webkuldemo,webkuldb etx.
-save :We can save our current terminal config using –save.
./odoo.py --xmlrpc-port 9069 --addons-path /opt/odoo9/openerp/addons,/opt/odoo9/addons,/opt/odoo9/customs --db-filter webkul.* -save # now currently used parameters entry will save in the default config file($HOME/. openerp_serverrc) for permanent . /odoo.py -c /etc/odoo9.conf --db-filter webkul.* -save # now currently used parameters will save in the mention config file for permanent .
Let’s Play with Odoo Shell
As other framework Odoo also provide shell interface for db access and data manipulation. I always love to use shell for database management instead of using GUI, odoo shell interface is too fast which save my priceless time. For accessing the shell interface of Odoo we should run ./odoo.py shell as below.
./odoo.py shell
dir() #['__builtins__', 'openerp']
This will provide us the openerp package for shell.
Below i am posting a example of db service access using shell.
from openerp.service import db #help(db) db.list_dbs() #Prove list of avalibale db db.exp_drop('dbname') #Drop an existing db db.exp_duplicate_database('olddb','newdb') #Duplicate an existing db db.exp_create_database('dbname',None,'en_US','username','password') #Create a new db
In case of manipulating the Odoo data you must pass db name with shell like:
./odoo.py shell -d dbname
dir() #['__builtins__', 'env', 'openerp', 'self']
Now as self and environment is available so we can access the odoo models data as below:
model = self.env['res.partner'] domain=[] model.search(domain) #res.partner(1, 7, 6, 4, 29, 8) vals = dict(name='demouser', email='demo') model.partner(vals) #res.partner(30,)
Hope you enjoyed this post, I’d be very grateful if you’d write your opinions, comments and suggestions to keep the page updated and interesting.
You also like our post on external dependencies and Odoo hook.
4 comments
Regards,
Mohit
Webkul
self.env[‘res.partner’].create({‘name’:’Testing Partner’})
It result something like this : res.partner(3,).
After quit from python prompt and check directly to Odoo and database, it never created. Am I missing something?