This question has been flagged
1 Reply
3775 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?

Avatar
Discard
Author Best Answer

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
Avatar
Discard