Book Image

Odoo 14 Development Cookbook - Fourth Edition

By : Parth Gajjar, Alexandre Fayolle, Holger Brunn, Daniel Reis
5 (2)
Book Image

Odoo 14 Development Cookbook - Fourth Edition

5 (2)
By: Parth Gajjar, Alexandre Fayolle, Holger Brunn, Daniel Reis

Overview of this book

With its latest iteration, the powerful Odoo framework released a wide variety of features for rapid application development. This updated Odoo development cookbook will help you explore the new features in Odoo 14 and learn how to use them to develop Odoo applications from scratch. You'll learn about the new website concepts in Odoo 14 and get a glimpse of Odoo's new web-client framework, the Odoo Web Library (OWL). Once you've completed the installation, you'll begin to explore the Odoo framework with real-world examples. You'll then create a new Odoo module from the ground up and progress to advanced framework concepts. You'll also learn how to modify existing applications, including Point of Sale (POS) applications. This book isn't just limited to backend development; you'll discover advanced JavaScript recipes for creating new views and widgets. As you progress, you'll learn about website development and become a quality Odoo developer by studying performance optimization, debugging, and automated testing. Finally, you'll delve into advanced concepts such as multi-website, In-App Purchasing (IAP), Odoo.sh, the IoT Box, and security. By the end of the book, you'll have all the knowledge you need to build impressive Odoo applications and you'll be well versed in development best practices that will come in handy when working with the Odoo framework.
Table of Contents (26 chapters)

Installing and upgrading local add-on modules

The core functionality of Odoo comes from its add-on modules. You have a wealth of add-ons available as part of Odoo itself, as well as add-on modules that you can download from the app store or that have been written by yourself.

In this recipe, we will demonstrate how to install and upgrade add-on modules through the web interface and from the command line.

The main benefits of using the command line for these operations include being able to act on more than one add-on at a time and having a clear view of the server logs as the installation or update progresses, which is very useful when in development mode or when scripting the installation of an instance.

Getting ready

Make sure that you have a running Odoo instance with its database initialized and the add-ons path properly set. In this recipe, we will install/upgrade a few add-on modules.

How to do it…

There are two possible methods to install or update add-ons—you can use the web interface or the command line.

From the web interface

To install a new add-on module in your database using the web interface, perform the following steps:

  1. Connect to the instance using the Administrator account and open the Apps menu:
    Figure 2.1 – List of Odoo apps

    Figure 2.1 – List of Odoo apps

  2. Use the search box to locate the add-on you want to install. Here are a few instructions to help you with this task:
    • Activate the Not Installed filter.
    • If you're looking for a specific functionality add-on rather than a broad functionality add-on, remove the Apps filter.
    • Type a part of the module name in the search box and use this as a Module filter.
    • You may find that using the list view gives something more readable.
  3. Click on the Install button under the module name in the card.

Note that some Odoo add-on modules have external Python dependencies. If Python dependencies are not installed in your system, then Odoo will abort the installation and it will show the following dialog:

Figure 2.2 – Warning for external library dependency

Figure 2.2 – Warning for external library dependency

To fix this, just install the relevant Python dependencies on your system.

To update a pre-installed module in your database, perform the following steps:

  1. Connect to the instance using the Administrator account.
  2. Open the Apps menu.
  3. Click on Apps:
    Figure 2.3 – Odoo apps list

    Figure 2.3 – Odoo apps list

  4. Use the search box to locate the add-on you want to install. Here are a few tips:
    • Activate the Installed filter.
    • If you're looking for a specific functionality add-on rather than a broad functionality add-on, remove the Apps filter.
    • Type a part of the add-on module name into the search box and then press Enter to use this as a Module filter. For example, type CRM and press Enter to search CRM apps.
    • You may find that using the list view gives you something more readable.
  5. Click on the three dots in the top right-corner of the card and click on the Upgrade option:
Figure 2.4 – Drop-down link for upgrading the module

Figure 2.4 – Drop-down link for upgrading the module

Activate developer mode to see the technical name of the module. See Chapter 1, Installing the Odoo Development Environment, if you don't know how to activate developer mode:

Figure 2.5 – Application's technical names

Figure 2.5 – Application's technical names

After activating developer mode, it will show the module's technical name in red. If you are using Odoo Community Edition, you will see some extra apps with the Upgrade button. Those apps are Odoo Enterprise Edition apps, and in order to install/use them, you need to purchase a license.

From the command line

To install new add-ons in your database, perform the following steps:

  1. Find the names of the add-ons. This is the name of the directory containing the _manifest_.py file, without the leading path.
  2. Stop the instance. If you are working on a production database, make a backup.
  3. Run the following command:
    $ odoo/odoo-bin -c instance.cfg -d dbname -i addon1,addon2 \
    --stop-after-init

    You may omit -d dbname if this is set in your configuration file.

  4. Restart the instance.

To update an already installed add-on module in your database, perform the following steps:

  1. Find the name of the add-on module to update; this is the name of the directory containing the _manifest_.py file, without the leading path.
  2. Stop the instance. If you are working on a production database, make a backup.
  3. Run the following command:
    $ odoo/odoo-bin -c instance.cfg -d dbname -u addon1 \
    --stop-after-init

    You may omit -d dbname if this is set in your configuration file.

  4. Restart the instance.

How it works…

The add-on module installation and update are two closely related processes, but there are some important differences, as highlighted in the following two sections.

Add-on installation

When you install an add-on, Odoo checks its list of available add-ons for an uninstalled add-on with the supplied name. It also checks for the dependencies of that add-on and, if there are any, it will recursively install them before installing the add-on.

The installation process of a single module consists of the following steps:

  1. If there are any, run the add-on preinit hook.
  2. Load the model definitions from the Python source code and update the database structure, if necessary (refer to Chapter 4, Application Models, for details).
  3. Load the data files of the add-on and update the database contents, if necessary (refer to Chapter 6, Managing Module Data, for details).
  4. Install the add-on demo data if demo data has been enabled in the instance.
  5. If there are any, run the add-on postinit hook.
  6. Run a validation of the view definitions of the add-on.
  7. If demo data is enabled and a test is enabled, run the tests of the add-on (refer to Chapter 18, Automated Test Cases, for details).
  8. Update the module state in the database.
  9. Update the translations in the database from the add-on's translations (refer to Chapter 11, Internationalization, for details).

    Note

    The preinit and postinit hooks are defined in the _manifest_.py file using the pre_init_hook and post_init_hook keys, respectively. These hooks are used to invoke Python functions before and after the installation of an add-on module. To learn more about init hooks, refer to Chapter 3, Creating Odoo Add-On Modules.

Add-on update

When you update an add-on, Odoo checks in its list of available add-on modules for an installed add-on with the given name. It also checks for the reverse dependencies of that add-on (these are the add-ons that depend on the updated add-on). If any, it will recursively update them, too.

The update process of a single add-on module consists of the following steps:

  1. Run the add-on module's pre-migration steps, if any (refer to Chapter 6, Managing Module Data, for details).
  2. Load the model definitions from the Python source code and update the database structure if necessary (refer to Chapter 4, Application Models, for details).
  3. Load the data files of the add-on and update the database's contents if necessary (refer to Chapter 6, Managing Module Data, for details).
  4. Update the add-on's demo data if demo data is enabled in the instance.
  5. If your module has any migration methods, run the add-on post-migration steps (refer to Chapter 6, Managing Module Data, for details).
  6. Run a validation of the view definitions of the add-on.
  7. If demo data is enabled and a test is enabled, run the tests of the add-on (refer to Chapter 18, Automated Test Cases, for details).
  8. Update the module state in the database.
  9. Update the translations in the database from the add-on's translations (refer to Chapter 11, Internationalization, for details).

    Note

    Note that updating an add-on module that is not installed does nothing at all. However, installing an add-on module that is already installed reinstalls the add-on, which can have some unintended effects with some data files that contain data that is supposed to be updated by the user and not updated during the normal module update process (refer to the Using the noupdate and forcecreate flags recipe in Chapter 6, Managing Module Data). There is no risk of error from the user interface, but this can happen from the command line.

There's more…

Be careful with dependency handling. Consider an instance where you want to have the sale, sale_stock, and sale_specific add-ons installed, with sale_specific depending on sale_stock, and sale_stock depending on sale. To install all three, you only need to install sale_specific, as it will recursively install the sale_stock and sale dependencies. To update all three, you need to update sale, as this will recursively update the reverse dependencies, sale_stock and sale_specific.

Another tricky part with managing dependencies is when you add a dependency to an add-on that already has a version installed. Let's understand this by continuing with the previous example. Imagine that you add a dependency on stock_dropshipping in sale_specific. Updating the sale_specific add-on will not automatically install the new dependency, and neither will requesting the installation of sale_specific. In this situation, you can get very nasty error messages because the Python code of the add-on is not successfully loaded, but the data of the add-on and the models' tables in the database are present. To resolve this, you need to stop the instance and manually install the new dependency.