Read More
Read More
Menu Close

    A Beginner Guide For Odoo CLI(Command Line Interface)

    This article gives an overview of how we as a developer   work with Odoo Command Line.
    In order to explain it, we have already install odoo v9 series GitHub repository and create a custom configuration file (at /opt/odoo9.conf) with minimal configuration.

    philosophy of  odoo scripts

    There are 3 three script file available of command line access .

    1. odoo.py(python scripts)
    2. openerp-server(bash scripts)
    3. 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.

    Searching for an experienced
    Odoo Company ?
    Read More

    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

    For changing the default port we can use –xmlrpc-port <port>
    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.
    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.

    1. –test-enable (for all module tests)
    2. –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 .

    . . .
    Discuss on Helpdesk

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    4 comments

  • THIAGO MACÊDO
    If you want to update all modules, is there a way?
    • mohit budakoti
      Yes, you can update all module by passing -u base, in the odoo start command parameter. This will update all the modules as base being the core module of every module.

      Regards,
      Mohit
      Webkul

  • Mas Jay
    I’m trying the way to create partner using this command :

    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?

    • odoo Webkul
      While using the Odoo CLI for data manipulation , you will have to commit the cursor [self._cr.commit()]
  • Back to Top

    Table of Content