콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
1 회신
5057 화면

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?

아바타
취소
작성자 베스트 답변

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
아바타
취소
관련 게시물 답글 화면 활동
2
7월 25
4442
2
12월 24
7629
2
11월 24
28397
2
5월 24
7364
3
3월 24
6793