Odoo 14 Development Cookbook - Fourth Edition

5 (3 reviews total)
By Parth Gajjar , Alexandre Fayolle , Holger Brunn and 1 more
  • Instant online access to over 7,500+ books and videos
  • Constantly updated with 100+ new titles each month
  • Breadth and depth in over 1,000+ technologies
  1. Chapter 1: Installing the Odoo Development Environment

About 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.

Publication date:
December 2020
Publisher
Packt
Pages
784
ISBN
9781800200319

 

Chapter 1: Installing the Odoo Development Environment

There are several ways to set up an Odoo development environment. This chapter proposes one of them; you will certainly find a number of other tutorials on the web explaining other approaches. Keep in mind that this chapter is about a development environment that has different requirements from a production environment.

If you are new to Odoo development, you must know about certain aspects of the Odoo ecosystem. The first recipe will give you a brief introduction to the Odoo ecosystem, and then we will move on to the installation of Odoo for development.

In this chapter, we will cover the following recipes:

  • Understanding the Odoo ecosystem
  • Easy installation of Odoo from source
  • Managing Odoo server databases
  • Storing the instance configuration in a file
  • Activating Odoo developer tools
  • Updating the add-on modules list
 

Understanding the Odoo ecosystem

Odoo provides the developer with out-of-the-box modularity. Its powerful framework helps the developer to build projects very quickly. There are various characters in the Odoo ecosystem that you should be familiar with before embarking on your journey of becoming a successful Odoo developer.

Odoo editions

Odoo comes with two different editions. The first one is the Community Edition, which is open source, and the second one is the Enterprise Edition, which has licensing fees. Unlike other software vendors, Odoo Enterprise Edition is just a pack of extra applications that adds extra features or new apps to the Community Edition. Basically, the Enterprise Edition runs on top of the Community Edition. The Community Edition comes under the Lesser General Public License v3.0 (LGPLv3) license and comes with all of the basic Enterprise Resource Planning (ERP) applications, such as sales, Customer Relationship Management (CRM), invoicing, purchases, and website builder. Alternatively, the Enterprise Edition comes with the Odoo Enterprise Edition License, which is a proprietary license. Odoo Enterprise Edition has a number of advanced features, such as full accounting, studio, Voice over Internet Protocol (VoIP), mobile responsive design, e-sign, marketing automation, delivery and banking integrations, IoT, and more. The Enterprise Edition also provides you with unlimited bugfix support. The following diagram shows that the Enterprise Edition depends on the Community Edition, which is why you need the latter in order to use the former:

Figure 1.1 – Differences between the Community and Enterprise Editions

Figure 1.1 – Differences between the Community and Enterprise Editions

You can see a full comparison of both editions here: https://www.odoo.com/page/editions.

Note

Odoo has the largest number of community developers, which is why you will find a large number of third-party apps (modules) on the app store. Some of the free apps use an Affero General Public License version 3 (AGPLv3). You cannot use the proprietary license on your app if your application has dependencies on such apps. Apps with an Odoo proprietary license can be developed only on modules that have LGPL or other proprietary licenses.

Git repositories

The entire code base of Odoo is hosted on GitHub. You can post bugs/issues for stable versions here. You can also propose a new feature by submitting Pull Requests (PR). There are several repositories in Odoo. See the following table for more information:

Table 1.1

Table 1.1

Every year, Odoo releases one major (Long-Term Support (LTS)) version and a few minor versions. Minor versions are mostly used in Odoo's online SaaS service, meaning that Odoo SaaS users get early access to these features. Major version branches have names such as 14.0, 13.0, and 12.0, while minor version branches have names such as saas-14.1, and saas-14.2 on GitHub. These minor versions are mostly used for Odoo's SaaS platform. The master branch is under development and is unstable, so it is advisable not to use this for production since it might break down your database.

Runbot

Runbot is Odoo's automated testing environment. Whenever there is a new commit in Odoo's GitHub branch, Runbot pulls those latest changes and creates the builds for the last four commits. Here, you can test all stable and in-development branches. You can even play with the Enterprise Edition and its development branches.

Every build has a different background color, which indicates the status of the test cases. A green background color means that all of the test cases run successfully and you can test that branch, while a red background color means that some test cases have failed on this branch and some features might be broken on that build. You can view the logs for all test cases, which show exactly what happens during installation. Every build has two databases. The all database has all of the modules installed on it, while the base database only has base Odoo modules installed. Every build is installed with basic demo data, and therefore you can test it quickly without extra configurations.

Note

You can access Runbot from the following URL: http://runbot.odoo.com/runbot.

The following credentials can be used to access any Runbot build:

  • Login ID: admin Password: admin
  • Login ID: demo Password: demo
  • Login ID: portal Password: portal

    Note

    This is a public testing environment, so sometimes it is possible that other users are using/testing the same branch that you are testing.

Odoo app store

Odoo launched the app store a few years back, and this was an instant hit. Right now, there are over 22,000+ different apps hosted there. In the app store, you will find lots of free and paid applications for different versions. This includes specific solutions for different business verticals, such as education, food industries, and medicine. It also includes apps that extend or add new features to existing Odoo applications. The app store also provides numerous beautiful themes for the Odoo website builder. In Chapter 3, Creating Odoo Add-On Modules, we will look at how you can set pricing and currency for your custom module.

You can access the Odoo app store via the following URL: https://www.odoo.com/apps.

You can access the Odoo themes via the following URL: https://www.odoo.com/apps/themes.

Note

Odoo has open sourced several themes with versions 13 and 14. Note that these were paid themes in previous versions. This means that, in Odoo versions 13 and 14, you can download and use those beautiful themes at no extra cost.

Odoo Community Association

Odoo Community Association (OCA) is a non-profit organization that develops/manages community-based Odoo modules. All OCA modules are open source and maintained by Odoo community members. Under the OCA's GitHub account, you will find multiple repositories for different Odoo applications. Apart from Odoo modules, it also contains various tools, a migration library, accounting localizations, and so on.

Here is the URL for OCA's official GitHub account: https://github.com/OCA.

Official Odoo help forum

Odoo has a very powerful framework, and tons of things can be achieved just by using/activating options or by following specific patterns. Consequently, if you run into some technical issues or if you are not sure about some complex cases, then you can post your query on Odoo's official help forum. Lots of developers are active on this forum, including some official Odoo employees.

You can search your questions or post your new questions at the following URL: https://help.odoo.com.help.odoo.com.

Odoo's eLearning platform

Recently, Odoo has launched a new eLearning platform. This platform has lots of videos that explain how to use different Odoo applications. At the time of writing this book, this platform does not have technical videos, just functional ones.

Here is the URL for Odoo's eLearning platform: https://www.odoo.com/slides.

 

Easy installation of Odoo from source

It is highly recommended to use the Linux Ubuntu operating system for the installation of Odoo, since this is the operating system that Odoo uses for all its tests, debugging, and installations of Odoo Enterprise, in addition to the fact that most developers of Odoo also use GNU/Linux distributions, and is much more likely to get support from the Odoo community for OS-level issues that occur in GNU/Linux than Windows or macOS.

It is also recommended to develop Odoo add-on modules using the same environment (the same distribution and the same version) as the one that will be used in production. This will avoid nasty surprises, such as discovering on the day of deployment that a library has a different version than expected, with a slightly different and incompatible behavior. If your workstation is using a different OS, a good approach is to set up a Virtual Machine (VM) on your workstation and install a GNU/Linux distribution in the VM.

Note

Ubuntu is available as an app in Microsoft Store so you can use that too, if you do not want to switch to Ubuntu OS.

For this book, we will be using Ubuntu Server 18.04 LTS, but you can use any another Debian GNU/Linux OS. Whatever Linux distribution you choose, you should have some notion of how to use it from the command line, and having knowledge of system administration will certainly not do any harm.

Getting ready

We are assuming that you have Ubuntu 18.04 up and running and that you have an account with root access or that sudo has been configured. In the following sections, we will install Odoo's dependencies and download Odoo's source code from GitHub.

Note

Some of the configurations require a system login username, so we will use $(whoami) whenever a login username is required in a command line. This is a shell command that will substitute your login in the command you are typing.

Some operations will definitely be easier if you have a GitHub account. If you don't have one already, go to https://github.com and create one.

How to do it...

To install Odoo from source, perform the following steps:

  1. Run the following commands to install the main dependencies:

    $ sudo apt-get update

    $ sudo apt install git python3-pip build-essential wget python3-dev python3-venv python3-wheel libxslt-dev libzip-dev libldap2-dev libsasl2-dev python3-setuptools libpng12-0 libjpeg-dev gdebi -y

  2. Download and install wkhtmltopdf:

    $ wget https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/0.12.5/wkhtmltox_0.12.5-1.trusty_amd64.deb

    $ sudo dpkg -i wkhtmltox_0.12.5-1.trusty_amd64.deb

    If you find errors in a previous command, force install the dependencies with the following command:

    $ sudo apt-get install -f

  3. Now, install the PostgreSQL database:

    $ sudo apt install postgresql -y

  4. Configure PostgreSQL:

    $ sudo -u postgres createuser --superuser $(whoami)

  5. Configure git:

    $ git config --global user.name "Your Name"

    $ git config --global user.email [email protected]

  6. Clone the Odoo code base:

    $ mkdir ~/odoo-dev

    $ cd ~/odoo-dev

    $ git clone -b 14.0 --single-branch --depth 1 https://github.com/odoo/odoo.git

  7. Create an odoo-14.0 virtual environment and activate it:

    $ python3 -m venv ~/venv-odoo-14.0

    $ source ~/venv-odoo-14.0/bin/activate

  8. Install the Python dependencies of Odoo in venv:

    $ cd ~/odoo-dev/odoo/

    $ pip3 install -r requirements.txt

  9. Create and start your first Odoo instances:

    $ createdb odoo-test

    $ python3 odoo-bin -d odoo-test –i base --addons-path=addons --db-filter=odoo-test$

  10. Point your browser to http://localhost:8069 and authenticate it by using the admin account and using admin as the password.

    Note

    If you need RTL support, please install node and rtlcss via the following command:sudo apt-get install nodejs npm -y sudo npm install -g rtlcss

How it works...

In step 1, we installed several core dependencies. These dependencies include various tools, such as git, pip3, wget, Python setup tools, and more. These core tools will help us install other Odoo dependencies using simple commands.

In step 2, we downloaded and installed the wkhtmltopdf package, which is used in Odoo to print PDF documents such as sale orders, invoices, and other reports. Odoo 14.0 needs version 0.12.5 of wkhtmltopdf, and that exact version might be not included in the current Linux distributions. Fortunately for us, the maintainers of wkhtmltopdf provide pre-built packages for various distributions at http://wkhtmltopdf.org/downloads.html and we have downloaded and installed it from that URL.

PostgreSQL configuration

In step 3, we installed the PostgreSQL database.

In step 4, we created a new database user with the login name of your system. $(whoami) is used to fetch your login name, and the -s option is used to give super user rights. Let's see why we need these configurations.

Odoo uses the psycopg2 Python library to connect with a PostgreSQL database. To access a PostgreSQL database with the psycopg2 library, Odoo uses the following values by default:

  • By default, psycopg2 tries to connect to a database with the same username as the current user on local connections, which enables password-less authentication (this is good for the development environment).
  • The local connection uses Unix domain sockets.
  • The database server listens on port 5432.

That's it! Your PostgreSQL database is now ready to be connected with Odoo.

As this is a development server, we have given --superuser rights to the user. It is OK to give the PostgreSQL user more rights as this will be your development instance. For a production instance, you can use the --createdb command line instead of --superuser to restrict rights. The –superuser rights in a production server will give additional leverage to an attacker exploiting a vulnerability in some part of the deployed code.

If you want to use a database user with a different login, you will need to provide a password for the user. This is done by passing the --pwprompt flag on the command line when creating the user, in which case the command will prompt you for the password.

If the user has already been created and you want to set a password (or modify a forgotten password), you can use the following command:

$ psql -c "alter role $(whoami) with password 'newpassword'"

If this command fails with an error message saying that the database does not exist, it is because you did not create a database named after your login name in step 4 of this recipe. That's fine; just add the --dbname option with an existing database name, such as --dbname template1.

Git configuration

For the development environment, we are using Odoo sourced from GitHub. With git, you can easily switch between different Odoo versions. Also, you can fetch the latest changes with the git pull command.

In step 5, we configured your git user.

In step 6, we downloaded the source code from Odoo's official GitHub repository. We have used the git clone command to download Odoo's source code. We have used a single branch as we only want a branch for the 14.0 version. Also, we have used --depth 1 to avoid downloading the full commit history of the branch. These options will download the source code very quickly, but if you want, you can omit those options.

Odoo developers also propose nightly builds, which are available as tarballs and distribution packages. The main advantage of using git clone is that you will be able to update your repository when new bug fixes are committed in the source tree. You will also be able to easily test any proposed fixes and track regressions so that you can make your bug reports more precise and helpful for developers.

Note

If you have access to the enterprise edition source code, you can download that too in a separate folder under the ~/odoo-dev directory.

Virtual environments

Python virtual environments, or venv for short, are isolated Python workspaces. These are very useful to Python developers because they allow different workspaces with different versions of various Python libraries to be installed, possibly on different Python interpreter versions.

You can create as many environments as you wish using the python3 -m venv ~/newvenv command. This will create a newvenv directory in the specified location, containing a bin/ subdirectory and a lib/python3.6 subdirectory.

In step 7, we created a new virtual environment in the ~/venv-odoo-14.0 directory. This will be our isolated Python environment for Odoo, and all of Odoo's Python dependencies will be installed in this environment.

To activate the virtual environment, we need to use the source command. With the source ~/venv-odoo-14.0/bin/activate command, we have activated the virtual environment.

Installing Python packages

Odoo's source code has a list of Python dependencies in requirements.txt. In step 8, we installed all those requirements via the pip3 install command.

That's it. Now you can run the Odoo instance.

Starting the instance

Now comes the moment you've been waiting for. To start our first instance, in step 9, we first created a new empty database, used the odoo-bin script, and then started the Odoo instance with the following command:

python3 odoo-bin -d odoo-test -i base --addons-path=addons --db-filter=odoo-test$

You can also omit python3 by using ./ before odoo-bin as it is an executable Python script, as follows:

./odoo-bin -d odoo-test –i base --addons-path=addons --db-filter=odoo-test$

With odoo-bin, a script with the following command-line arguments are used:

  • -d database_name: Use this database by default.
  • --db-filter=database_name$: Only try to connect to databases that match the supplied regular expression. One Odoo installation can serve multiple instances that live in separate databases, and this argument limits the available databases. The trailing $ is important as the regular expression is used in match mode. This enables you to avoid selecting names starting with the specified string.
  • --addons-path=directory1,directory2,...: This is a comma- separated list of directories in which Odoo will look for add-ons. This list is scanned at instance creation time to populate the list of available add-on modules in the instance. If you want to use Odoo's Enterprise Edition, then add its directory with this option.
  • -i base: This is used to install a base module. This is required when you have created a database via the command line.

If you are using a database user with a database login that is different from your Linux login, you need to pass the following additional arguments:

  • --db_host=localhost: Use a TCP connection to the database server.
  • --db_user=database_username: Use the specified database login.
  • --db_password=database_password: This is the password for authenticating against the PostgreSQL server.

To get an overview of all available options, use the --help argument. We will see more of the odoo-bin script later in this chapter.

When Odoo is started on an empty database, it will first create the database structure that's needed to support its operations. It will also scan the add-ons path to find the available add-on modules and insert some into the initial records in the database. This includes the admin user with the default admin password, which you will use for authentication.

Pointing your web browser to http://localhost:8069/ leads you to the login page of your newly created instance, as shown in the following screenshot:

Figure 1.2 – Login screen of the Odoo instance

Figure 1.2 – Login screen of the Odoo instance

This is due to the fact that Odoo includes an HTTP server. By default, it listens on all local network interfaces on TCP port 8069.

 

Managing Odoo server databases

When working with Odoo, all the data in your instance is stored in a PostgreSQL database. All the standard database management tools you are used to are available, but Odoo also proposes a web interface for some common operations.

Getting ready

We are assuming that your work environment is set up and that you have an instance running.

How to do it...

The Odoo database management interface provides tools to create, duplicate, remove, back up, and restore a database. There is also a way to change the master password, which is used to protect access to the database management interface.

Accessing the database management interface

To access the database, perform the following steps:

  1. Go to the login screen of your instance (if you are authenticated, log out).
  2. Click on the Manage Databases link. This will navigate to http://localhost:8069/web/database/manager (you can also point your browser directly to that URL):
Figure 1.3 – Database manager

Figure 1.3 – Database manager

Setting or changing the master password

If you've set up your instance with default values and haven't modified it yet, as we will explain in the following section, the database management screen will display a warning, telling you that the master password hasn't been set and will advise you to set one with a direct link:

Figure 1.4 – Master password warning

Figure 1.4 – Master password warning

To set the master password, perform the following steps:

  1. Click on the Set Master Password button. You will get a dialog box asking you to fill in the New Master Password field:
    Figure 1.5 – Setting a new master password dialog

    Figure 1.5 – Setting a new master password dialog

  2. Type in a non-straightforward new password and click Continue.

If the master password is already set, click on the Set Master Password button at the bottom of the screen to change it. In the displayed dialog box, type the previous master password and the new one and then click on Continue.

Note

The master password is the server configuration file under the admin_password key. If the server was started without specifying a configuration file, a new one will be generated in ~/.odoorc. Refer to the next recipe for more information about the configuration file.

Creating a new database

This dialog box can be used to create a new database instance that will be handled by the current Odoo server:

  1. In the database management window, click on the Create Database button, which can be found at the bottom of the screen. This will bring up the following dialog:
    Figure 1.6 – Creating a new database dialog

    Figure 1.6 – Creating a new database dialog

  2. Fill in the form, as follows:
    • Master Password: This is the master password for this instance.
    • Database Name: Input the name of the database you wish to create.
    • Email: Add your email address here; this will be your username later.
    • Password: Type in the password you want to set for the admin user of the new instance.
    • Phone Number: Set the phone number (optional).
    • Language: Select the language you wish to be installed by default in the new database in the drop-down list. Odoo will automatically load the translations for the selected language.
    • Country: Select the country of the main company in the drop-down list. Selecting this will automatically configure a few things, including company currency.
    • Demo data: Check this box to obtain demonstration data. This is useful for running interactive tests or setting up a demonstration for a customer, but it should not be checked for a database that is designed to contain production data.

      Note

      If you wish to use the database to run the automated tests of the modules (refer to Chapter 7, Debugging Modules), you need to have the demonstration data, as the vast majority of the automated tests in Odoo depend on these records in order to run successfully.

  3. Click on the Continue button and wait for a while until the new database is initialized. You will then be redirected to the instance and connected as the administrator.

    Troubleshooting

    If you are redirected to a login screen, this is probably because the --db-filter option was passed to Odoo and the new database name didn't match the new database name. Note that the odoo-bin start command does this silently, making only the current database available. To work around this, simply restart Odoo without the start command, as shown in the Easy installation of Odoo from source recipe of this chapter. If you have a configuration file (refer to the Storing the instance configuration in a file recipe later in this chapter), and then check that the db_filter option is unset or set to a value matching the new database name.

Duplicating a database

Often, you will have an existing database, and you will want to experiment with it to try a procedure or run a test, but without modifying the existing data. The solution here is simple: duplicate the database and run the test on the copy. Repeat this as many times as required:

  1. In the database management screen, click on the Duplicate Database link next to the name of the database you wish to clone:
    Figure 1.7 – Duplicate Database dialog

    Figure 1.7 – Duplicate Database dialog

  2. Fill in the form as follows:
    • Master Password: This is the master password of the Odoo server.
    • New Name: The name you want to give to the copy.
  3. Click on the Continue button.
  4. You can then click on the name of the newly created database in the database management screen to access the login screen for that database.

Removing a database

When you have finished your tests, you will want to clean up the duplicated databases. To do this, perform the following steps:

  1. In the database management screen, you will find the Delete button next to the name of the database. Clicking on it will bring up a dialog like the following screenshot:
    Figure 1.8 – Delete Database dialog

    Figure 1.8 – Delete Database dialog

  2. Fill in the form and complete the Master Password field, which is the master password of the Odoo server.
  3. Click on the Delete button.

    Caution! Potential data loss!

    If you selected the wrong database, and have no backup, there is no way to recover the lost data.

Backing up a database

To create a backup, perform the following steps:

  1. In the database management screen, you will find the Backup button next to the database name. Clicking on it will bring up dialog like the following screenshot:
    Figure 1.9 – Backup Database dialog

    Figure 1.9 – Backup Database dialog

  2. Fill in the form as follows:
    • Master Password: This is the master password of the Odoo server.
    • Backup Format: Always use zip for a production database, as this is the only real full backup format. Only use the pg_dump format for a development database when you don't really care about the file store.
  3. Click on the Backup button. The backup file will then be downloaded to your browser.

Restoring a database backup

If you need to restore a backup, this is what you need to do:

  1. In the database management screen, you will find a Restore Database button at the bottom of the screen. Clicking on it will bring up a dialog like the following screenshot:
    Figure 1.10 – Restore Database dialog

    Figure 1.10 – Restore Database dialog

  2. Fill in the form as follows:
    • Master Password: This is the master password of the Odoo server.
    • File: This is a previously downloaded Odoo backup.
    • Database Name: Provide the name of the database in which the backup will be restored. The database must not exist on the server.
    • This database might have been moved or copied: Choose This database was moved if the original database was on another server or if it has been deleted from the current server. Otherwise, choose This database is a copy, which is the safe default option.
  3. Click on the Continue button.

    Note

    It isn't possible to restore a database on top of itself. If you try to do this, you will get an error message (Database restore error: Database already exists). You need to remove the database first.

How it works...

These features, apart from the Change master password screen, run PostgreSQL administration commands on the server and report back through the web interface.

The master password is a very important piece of information that only lives in the Odoo server configuration file and is never stored in the database. There used to be a default value of admin, but using this value is a security liability, which is well-known. In Odoo v9 and later, this is identified as an unset master password, and you are urged to change it when accessing the database administration interface. Even if it is stored in the configuration file under the admin_passwd entry, this is not the same as the password of the admin user; these are two independent passwords. The master password is set for an Odoo server process, which itself can handle multiple database instances, each of which has an independent admin user with their own password.

Security considerations

Remember that we are considering a development environment in this chapter. The Odoo database management interface is something that needs to be secured when you are working on a production server, as it gives access to a lot of sensitive information, especially if the server hosts Odoo instances for several different clients.

To create a new database, Odoo uses the PostgreSQL createdb utility and calls the internal Odoo function to initialize the new database in the same way as when you start Odoo on an empty database.

To duplicate a database, Odoo uses the --template option of createdb, passing the original database as an argument. This essentially duplicates the structure of the template database in the new database using internal and optimized PostgreSQL routines, which is much faster than creating a backup and restoring it (especially when using the web interface, which requires downloading the backup file and uploading it again).

Backup and restore operations use the pg_dump and pg_restore utilities, respectively. When using the zip format, the backup will also include a copy of the file store that contains a copy of the documents when you configure Odoo to not keep these in the database, which is the default option in 14.0. Unless you change it, these files reside in ~/.local/share/Odoo/filestore.

If the backup gets large, downloading it may fail. This is either because the Odoo server itself is unable to handle the large file in memory or because the server is running behind a reverse proxy because there is a limit to the size of HTTP responses that were set in the proxy. Conversely, for the same reasons, you will likely experience issues with the database restore operation. When you start running into these issues, it is time to invest in a more robust external backup solution.

There's more...

Experienced Odoo developers generally don't use the database management interface and perform operations from the command line. To initialize a new database with demo data, for instance, the following single-line command can be used:

$ createdb testdb && odoo-bin -d testdb

The additional bonus of this command line is that you can request the installation of add-ons while you are using, for instance, -i sale,purchase,stock.

To duplicate a database, stop the server and run the following commands:

$ createdb -T dbname newdbname

$ cd ~/.local/share/Odoo/filestore # adapt if you have changed the data_dir

$ cp -r dbname newdbname

$ cd -

Note that, in the context of development, the file store is often omitted.

Note

The use of createdb -T only works if there are no active sessions on the database, which means that you have to shut down your Odoo server before duplicating the database from the command line.

To remove an instance, run the following command:

$ dropdb dbname

$ rm -rf ~/.local/share/Odoo/filestore/dbname

To create a backup (assuming that the PostgreSQL server is running locally), use the following command:

$ pg_dump -Fc -f dbname.dump dbname

$ tar cjf dbname.tgz dbname.dump ~/.local/share/Odoo/filestore/dbname

To restore the backup, run the following command:

$ tar xf dbname.tgz

$ pg_restore -C -d dbname dbname.dump

Caution!

If your Odoo instance uses a different user to connect to the database, you need to pass -U username so that the correct user is the owner of the restored database.

 

Storing the instance configuration in a file

The odoo-bin script has dozens of options, and it is tedious to remember them all, as well as remembering to set them properly when starting the server. Fortunately, it is possible to store them all in a configuration file and to only specify by hand the ones you want to alter, for example, for development.

How to do it...

For this recipe, perform the following steps:

  1. To generate a configuration file for your Odoo instance, run the following command:

    $ ./odoo-bin --save --config myodoo.cfg --stop-after-init

  2. You can add additional options, and their values will be saved in the generated file. All the unset options will be saved with their default value set. To get a list of possible options, use the following command:

    $ ./odoo-bin --help | less

    This will provide you with some help about what the various options perform.

  3. To convert from the command-line form to the configuration form, use the long option name, remove the leading dashes, and convert the dashes in the middle into underscores. --without-demo then becomes without_demo. This works for most options, but there are a few exceptions, which are listed in the following section.
  4. Edit the myodoo.cfg file (use the table in the following section for some parameters you may want to change). Then, to start the server with the saved options, run the following command:

    $ ./odoo-bin -c myodoo.cfg

    Note

    The --config option is commonly abbreviated as -c.

How it works...

At startup, Odoo loads its configuration in three passes. First, a set of default values for all options is initialized from the source code, then the configuration is parsed, and then any value that's defined in the file overrides the defaults. Finally, the command-line options are analyzed, and their values override the configuration that was obtained from the previous pass.

As we mentioned earlier, the names of the configuration variables can be found from the names of the command-line options by removing the leading dashes and converting the middle dashes into underscores. There are a few exceptions to this, notably the following:

Table 1.2

Table 1.2

Here is a list of options that are commonly set through the configuration file:

Table 1.3

Table 1.3

Here is a list of configuration options related to the database:

Table 1.4

Table 1.4

The parsing of the configuration file by Odoo is now using the Python ConfigParser module. However, the implementation in Odoo 11.0 has changed, and it is no longer possible to use variable interpolation. So, if you are used to defining values for variables from the values of other variables using the %(section.variable)s notation, you will need to change your habits and revert to explicit values.

Some options are not used in config files, but they are widely used during development:

Table 1.5

Table 1.5

 

Activating Odoo developer tools

When using Odoo as a developer, you need to know how to activate developer mode in the web interface so that you can access the technical settings menu and developer information. Enabling debug mode will expose several advance configuration options and fields. These options and fields are hidden in Odoo for better usability because they are not used on a daily basis.

How to do it...

To activate developer mode in the web interface, perform the following steps:

  1. Connect to your instance and authenticate as admin.
  2. Go to the Settings menu.
  3. Scroll to the bottom and locate the Developer Tools section:
    Figure 1.11 – Links to activate different developer modes

    Figure 1.11 – Links to activate different developer modes

  4. Click on the Activate the developer mode link.
  5. Wait for the UI to reload.

    Alternative way

    It is also possible to activate the developer mode by editing the URL. Before the # sign, insert ?debug=1. For example, if your current URL is http://localhost:8069/web#menu_id=102&action=94 and you want to enable developer mode, then you need to change that URL to http://localhost:8069/web?debug=1#menu_id=102&action=94. Furthermore, if you want debug mode with assets, then change the URL to http://localhost:8069/web?debug=assets#menu_id=102&action=94.

To exit developer mode, you can perform any one of the following operations:

  • Edit the URL and write ?debug=0 in the query string.
  • Use Deactivate the developer mode from the same place in the Settings menu.
  • Click on the bug icon in the top menu and click on the Leave Developer Tools option.

Lots of developers are using browser extensions to toggle debug mode. By using this, you can toggle debug mode quickly without accessing the settings menu. These extensions are available for Firefox and Chrome. Take a look at the following screenshot. It will help you to identify the plugin in the Chrome store:

Figure 1.12 – Browser extension for debug mode

Figure 1.12 – Browser extension for debug mode

Note

The behavior of debug mode has changed since Odoo v13. Since v13, the status of the debug mode is stored in session, implying that even if you have removed ?debug from the URL, debug mode will still be active.

How it works...

In developer mode, two things happen:

  • You get tooltips when hovering over a field in a form view or over a column in list view, providing technical information about the field (internal name, type, and so on)
  • A drop-down menu with a bug icon is displayed next to the user's menu in the top-right corner, giving access to technical information about the model being displayed, the various related view definitions, the workflow, custom filter management, and so on.

There is a variant of developer mode – Developer mode (with assets). This mode behaves like the normal developer mode, but additionally, the JavaScript and CSS code that's sent to the browser is not minified, which means that the web development tools of your browser are easy to use for debugging the JavaScript code (more on this in Chapter 15, Web Client Development).

Caution!

Test your add-ons both with and without developer mode, as the unminified versions of the JavaScript libraries can hide bugs that only bite you in the minified version.

 

Updating the add-on modules list

When you add a new module, Odoo is unaware of the new module. In order to list the module in Odoo, you will need to update module list. In this recipe, you will learn how to update the app list.

Getting ready

Start your instance and connect to it using the Administrator account. After doing this, activate developer mode (if you don't know how to activate developer mode, refer to Chapter 1, Installing the Odoo Development Environment).

How to do it…

To update the list of available add-on modules in your instance, you need to perform the following steps:

  1. Open the Apps menu.
  2. Click on Update Apps List:
    Figure 1.13 – Menu item to update the apps list

    Figure 1.13 – Menu item to update the apps list

  3. In the dialog, click on the Update button:
    Figure 1.14 – Dialog to update the apps list

    Figure 1.14 – Dialog to update the apps list

  4. At the end of the update, you can click on the Apps entry to see the updated list of available add-on modules. You will need to remove the default filter on Apps in the search box to see all of them.

How it works…

When the Update button is clicked, Odoo will read the add-ons path configuration variable. For each directory in the list, it will look for immediate subdirectories containing an add-on manifest file, which is a file named __manifest__.py that's stored in the add-on module directory. Odoo reads the manifest, expecting to find a Python dictionary. Unless the manifest contains a key installable instance set to False, the add-on module metadata is recorded in the database. If the module was already present, the information is updated. If not, a new record is created. If a previously available add-on module is not found, the record is not deleted from the list.

Note

An updated apps list is only required if you added the new add-on path after initializing the database. If you add the new add-on path to the configuration file before initializing the database, then there will be no need to update the module list manually.

To summarize what we have learned so far, after installing, you can start the Odoo server by using the following command line (if you are using a virtual environment, then you need to activate it first):

python3 odoo-bin -d odoo-test -i base --addons-path=addons --db-filter=odoo-test

Once you run the module, you can access Odoo from http://localhost:8069.

You can also use a configuration file to run Odoo as follows:

./odoo-bin -c myodoo.cfg

Once you start the Odoo server, you can install/update modules from the App menu.

About the Authors

  • Parth Gajjar

    Parth Gajjar is an Odoo expert with a deep understanding of the Odoo framework. He started his career at Odoo and spent 7 years in the R&D department at Odoo India. During his time at Odoo, he worked on several key features, including a marketing automation app, mobile application, report engine, domain builder, and more. He also worked as a code reviewer and helped manage the code quality of the new features. Later, he started his own venture named Droggol and now provides various development services related to Odoo. He loves working on Odoo and solving real-world business problems with different technologies. He often gives technical training to Odoo developers.

    Browse publications by this author
  • Alexandre Fayolle

    Alexandre Fayolle started working with Linux and free software in the mid-1990s and quickly became interested in the Python programming language. In 2012, he joined Camptocamp to share his expertise on Python, PostgreSQL, and Linux with the team implementing Odoo. He c

    Browse publications by this author
  • Holger Brunn

    Holger Brunn has been a fervent open source advocate since he came into contact with the open source market sometime in the nineties. He has programmed for ERP and similar systems in different positions since 2001. For the last 10 years, he has dedicated his time to TinyERP, which became OpenERP and evolved into Odoo. Currently, he works at Therp BV in the Netherlands as a developer and is an active member of the Odoo Community Association (OCA).

    Browse publications by this author
  • Daniel Reis

    Daniel Reis has had a long career in the IT industry, mostly as a consultant implementing business applications in variety of sectors, and today works for Securitas, a multinational security services provider. He has been working with Odoo (formerly OpenERP) since 2010, is an active contributor to the Odoo Community Association projects, is currently a member of the board of the Odoo Community Association, and collaborates with ThinkOpen Solutions, a leading Portuguese Odoo integrator.

    Browse publications by this author

Latest Reviews

(3 reviews total)
J'ai lu auparavant les livres concernant Odoo, LEarn Odoo, Cookbook Odoo12, et maintenant Je suis entrain de lire lae livre Dev Odoo 14, C'est génial comme d'abitude ! Reste Les autres Séries: Leran Odoo14, et le livre dév rapide ODoo14, puis , s'il y aura d'autres vidéos ça sera encore mieux!
Díky. Dobře utracené peníze.
Great good excelent, Is very good book
Book Title
Access this book, plus 7,500 other titles for FREE
Access now