官术网_书友最值得收藏!

  • Odoo Development Cookbook
  • Holger Brunn Alexandre Fayolle Daniel Reis
  • 589字
  • 2021-07-16 11:00:31

Creating and installing a new addon module

In this recipe, we will create a new module, make it available in our Odoo instance, and install it.

Getting ready

We will need an Odoo instance ready to use.

If the first recipe in Chapter 1, Installing the Odoo Development Environment, was followed, Odoo should be available at ~/odoo-dev/odoo. For explanation purposes, we will assume this location for Odoo, although any other location of your preference could be used.

We will also need a location for our Odoo modules. For the purpose of this recipe, we will use a local-addons directory alongside the odoo directory, at ~/odoo-dev/local-addons.

How to do it…

The following steps will create and install a new addon module:

  1. Change the working directory in which we will work and create the addons directory where our custom module will be placed:
    $ cd ~/odoo-dev
    $ mkdir local-addons
    
  2. Choose a technical name for the new module and create a directory with that name for the module. For our example we will use my_module:
    $ mkdir local-addons/my_module
    

    Note

    A module's technical name must be a valid Python identifier; it must begin with a letter, and only contain letters (preferably lowercase), numbers, and underscore characters.

  3. Make the Python module importable by adding an __init__.py file:
    $ touch local-addons/my_module/__init__.py
    
  4. Add a minimal module manifest, for Odoo to detect it. Create an __openerp__.py file with this line:
    {'name': 'My module'}
    
  5. Start your Odoo instance including our module directory in the addons path:
    $ odoo/odoo.py --addons-path=odoo/addons/,local-addons/
    

    Note

    If the --save option is added to the Odoo command, the addons path will be saved in the configuration file. Next time you start the server, if no addons path option is provided, this will be used.

  6. Make the new module available in your Odoo instance; log in to Odoo using admin, enable the Developer Mode in the About box, and in the Apps top menu select Update Apps List. Now Odoo should know about our Odoo module.
  7. Select the Apps menu at the top and, in the search bar in the top right, delete the default Apps filter and search for my_module. Click on its Install button and the installation will be concluded.

How it works…

An Odoo module is a directory containing code files and other assets. The directory name used is the module's technical name. The name key in the module manifest is its title.

The __openerp__.py file is the module manifest. It contains a Python dictionary with information about the module, the modules it depends on, and the data files that it will load.

In the example, a minimal manifest file was used, but in real modules, we will want to add a few other important keys. These are discussed in the next recipe, Completing the module manifest.

The module directory must be Python-importable, so it also needs to have an __init__.py file, even if it's empty. To load a module, the Odoo server will import it. This will cause the code in the __init__.py file to be executed, so it works as an entry point to run the module Python code. Because of this, it will usually contain import statements to load the module Python files and submodules.

Modules can be installed directly from the command line using the --init, or -i, option. In the past, we had to use the Update Module List to make it available to the Odoo instance. However, at this moment, this is done automatically when the --init or --update are used from the command line.

主站蜘蛛池模板: 昭平县| 乐都县| 文成县| 洪泽县| 邓州市| 永定县| 肥西县| 溆浦县| 沙坪坝区| 凤山市| 囊谦县| 探索| 容城县| 疏附县| 曲阜市| 利津县| 高雄县| 临夏市| 三原县| 郴州市| 阿拉善左旗| 浪卡子县| 永修县| 萨嘎县| 镇宁| 都昌县| 新田县| 綦江县| 金昌市| 林州市| 河间市| 桐庐县| 曲靖市| 永年县| 田阳县| 类乌齐县| 邢台县| 固原市| 久治县| 阆中市| 日喀则市|