Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

Is it possible to add a custom cli command in a custom module?

By
Aidas Bendoraitis
on 11/21/16, 2:30 PM 106 views

I am coming from a Django world, where you can create custom management commands for custom actions which for example can be put under cron jobs.

Is there something similar in Odoo? If it is possible to add a cli command in an addon, then where to put it so that it is recognized by the system?

0
Aidas Bendoraitis
On 11/21/16, 4:03 PM

OK. I got it working myself by analyzing the source code.

To do that you need a cli module inside your custom module. Then a python file mycommand.py inside of it.

The mycommand.py contains something like this:

# -*- coding: UTF-8 -*-
from __future__ import unicode_literals
from odoo.cli.command import Command

class MyCommand(Command):
    """Run a custom command"""
    def run(self, args):
        print "Custom command 'mycommand' worked.\n" 

To get it correctly imported, you need the following in the __init__.py of your module:

from . import cli

And then you need the following in the cli/__init__.py:

import mycommand

To run the custom command, you need to type:

$ odoo --addons-path=/absolute/path/to/my/custom/addons mycommand
custom command 'mycommand' worked.

Here the custom command name "mycommand" is the lowercase name of the class inheriting from Command, i.e. "MyCommand". Note that the order of parameters passed to the odoo script matters: if you put the --addons-path after the custom command name, it won't work.

Interestingly, if you want to also pass odoo configuration to the command, it should go at the end of your parameters like this:

$ odoo --addons-path=/absolute/path/to/my/custom/addons mycommand -c odoo.conf

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 11/21/16, 2:30 PM
Seen: 106 times
Last updated: 11/21/16, 4:03 PM