Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Odoo Development Cookbook
Odoo Development Cookbook

Odoo Development Cookbook: Build effective business applications using the latest features in Odoo 17 , Fifth Edition

Arrow left icon
Profile Icon Daudi Profile Icon Daniel Reis Profile Icon Holger Brunn Profile Icon Fayolle Profile Icon Gajjar Profile Icon Vora +2 more Show less
Arrow right icon
$27.98 $39.99
Full star icon Full star icon Full star icon Full star icon Half star icon 4.2 (10 Ratings)
eBook May 2024 774 pages 5th Edition
eBook
$27.98 $39.99
Paperback
$49.99
Subscription
Free Trial
Renews at $19.99p/m
Arrow left icon
Profile Icon Daudi Profile Icon Daniel Reis Profile Icon Holger Brunn Profile Icon Fayolle Profile Icon Gajjar Profile Icon Vora +2 more Show less
Arrow right icon
$27.98 $39.99
Full star icon Full star icon Full star icon Full star icon Half star icon 4.2 (10 Ratings)
eBook May 2024 774 pages 5th Edition
eBook
$27.98 $39.99
Paperback
$49.99
Subscription
Free Trial
Renews at $19.99p/m
eBook
$27.98 $39.99
Paperback
$49.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Table of content icon View table of contents Preview book icon Preview Book

Odoo Development Cookbook

Installing the Odoo Development Environment

To begin our Odoo development journey, we must set up our development environment by installing Odoo using source code that we can use to enhance, debug, and improve our development skills. There are several ways to set up an Odoo development environment, but this chapter proposes the best of them. You will find several other tutorials on the web explaining the other approaches. Keep in mind that this chapter is about setting up a development environment that has different requirements from a production environment; production has different parameters we must set in the config file, as per the volume of data and number of users in the system. We will cover configuration file parameters and their usage in this chapter.

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, after which we will install Odoo for development purposes.

In this chapter, we will cover the following recipes:

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

Technical requirements

All the code that’s used in this chapter can be downloaded from this book’s GitHub repository at https://github.com/PacktPublishing/Odoo-17-Development-Cookbook-Fifth-Edition/tree/main/Chapter01.

Understanding the Odoo ecosystem

Odoo provides developers with out-of-the-box modularity and its powerful framework helps them build projects 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.

Let’s assume you have a system with 4 CPU cores, 8 GB of RAM, and 30 concurrent Odoo users.

To determine the number of workers needed, divide the number of users by 6. In this case, 30 users divided by 6 equals 5, which is the theoretical number of workers required.

To calculate the theoretical maximum number of workers, multiply the number of CPU cores by 2 and add 1. For 4 CPU cores, (4 * 2) + 1 equals 9, which is the theoretical maximum number of workers.

Based on these calculations, you can use 5 workers for the Odoo users and an additional worker for the cron worker, making a total of 6 workers.

To estimate the RAM consumption, use the following formula:

RAM = Number of workers * ((0.8 * 150) + (0.2 * 1024))

In this case, 6 workers multiplied by ((0.8 * 150) + (0.2 * 1024)) equals approximately 2 GB of RAM.

Therefore, based on these calculations, the Odoo installation will require around 2 GB of RAM.

Odoo editions

Odoo comes in two different editions. The first one is Community Edition, which is open source, while the second one is 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. The Enterprise Edition runs on top of the Community Edition. The Community Edition comes under the Lesser General Public License v3.0 (LGPLv3) and comes with all of the basic enterprise resource planning (ERP) applications, such as sales, customer relationship management (CRM), invoicing, purchases, and a website builder. Alternatively, the Enterprise Edition comes with the Odoo Enterprise Edition license, which is a proprietary license. Odoo Enterprise Edition has several advanced features, such as full accounting, studio, Voice over Internet Protocol (VoIP), mobile responsive design, e-sign, marketing automation, delivery and banking integrations, Internet of Things (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 to use the former:

Figure 1.1 – Differences between the Community Edition and Enterprise Edition

Figure 1.1 – Differences between the Community Edition and Enterprise Edition

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

Note

Odoo has the largest number of community developers among all open source ERPs on the market with 20K+ forks on GitHub, hence 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 only be developed 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 a pull request (PR). There are several repositories in Odoo. See the following table for more information:

Repositories

Purpose

https://github.com/odoo/odoo

This is the Community Edition of Odoo. It’s available publicly.

https://github.com/odoo/enterprise

This is the Enterprise Edition of Odoo. It’s available to official Odoo partners only.

https://github.com/odoo-dev/odoo

This is an ongoing development repository. It’s available publicly.

Table 1.1 – Odoo git repositories

Every year, Odoo releases a major release, which is a long-term support version that will last for 3 years, and a few minor versions. Minor versions are mostly used in Odoo’s online Software-as-a-Service (SaaS) offering, meaning that Odoo SaaS users get early access to these features. Major version branches have names such as 17.0, 16.0, 15.0, 14.0, 13.0, and 12.0, while minor version branches have names such as saas-17.1 and saas-17.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 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, so you can test it quickly without extra configurations.

Note

You can access Runbot at 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 other users might be using/testing the same branch that you are testing.

Odoo app store

Odoo launched the app store a few years back, and it was an instant hit. At the time of writing, over 39,000+ different apps are hosted there. You will find lots of free and paid applications for different versions here. 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, you will learn how to set pricing and currency for your custom module.

You can access the Odoo app store by going to https://www.odoo.com/apps.

You can access Odoo’s themes by going to https://www.odoo.com/apps/themes.

Note

Odoo has open sourced several themes after version 13 and now works with an advanced JavaScript called OWL. We will cover this in Chapter 16. Note that these were paid themes in previous versions. This means that, in Odoo versions 15 and 16, 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. OCA’s GitHub account contains 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 for questions or post your new questions by going to https://www.odoo.com/forum/help-1.

Odoo’s eLearning platform

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

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

Installing Odoo from the source

It is highly recommended that you use the Linux Ubuntu operating system to install Odoo since this is the operating system that Odoo uses for all its tests, debugging, and installations of Odoo Enterprise. Additionally, most Odoo developers use GNU/Linux distributions, so they are much more likely to get support from the Odoo community for operating system-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 operating system, 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 the Microsoft Store, so you can use that if you don’t want to switch to Ubuntu.

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

Getting ready

We are assuming that you have Ubuntu 22.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 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 the source, perform the following steps:

  1. Run the following commands to install the main dependencies:
    $ sudo apt-get update
    $ sudo apt install openssh-server fail2ban python3-pip python3-dev libxml2-dev libxslt1-dev zlib1g-dev libsasl2-dev libldap2-dev build-essential libssl-dev libffi-dev libmysqlclient-dev libpq-dev libjpeg8-dev liblcms2-dev libblas-dev libatlas-base-dev git curl python3-venv python3.10-venv fontconfig libxrender1 xfonts-75dpi xfonts-base -y
  2. Download and install wkhtmltopdf:
    $ wget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6.1-2/wkhtmltox_0.12.6.1-2.jammy_amd64.deb
    $ sudo dpkg -i wkhtmltox_0.12.6.1-2.jammy_amd64.deb

    If you encounter any errors after running the 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 -i -u postgres createuser -s  $(whoami)
    $ sudo su postgres
    $ psql
    alter user $(whoami) with password 'your_password';
    \q
    exit
  5. Configure git:
    $ git config --global user.name "Your Name"
    $ git config --global user.email youremail@example.com
  6. Clone the Odoo code base:
    $ mkdir ~/odoo-dev
    $ cd ~/odoo-dev
    $ git clone -b 17.0 --single-branch --depth 1 https://github.com/odoo/odoo.git
  7. Create an odoo-17.0 virtual environment and activate it:
    $ python3 -m venv ~/venv-odoo-17.0
    $ source ~/venv-odoo-17.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 by running 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 17.0 needs version 0.12.6.1 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.

After this, we configured PostgreSQL, which is used for Odoo’s database management.

PostgreSQL configuration

In step 3, we installed the PostgreSQL database.

In step 4, we created a new database user with the login name of our system. $(whoami) is used to fetch your login name, and the -s option is used to give superuser 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. Note that you can fetch the latest changes with the git pull command.

In step 5, we configured our git user.

In step 6, we downloaded the source code from Odoo’s official GitHub repository. We used the git clone command to download Odoo’s source code. We used a single branch as we only wanted a branch for the 17.0 version. We also 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 in a separate folder under the ~/odoo-dev directory.

Virtual environments

Python virtual environments, or venvs 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.10 subdirectory.

In step 7, we created a new virtual environment in the ~/venv-odoo-17.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. We used the source ~/venv-odoo-17.0/bin/activate command to activate 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 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:

./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 is 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 create the database structure that’s needed to support its operations. It will also scan the add-on 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 because 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 Manage Databases. This will navigate you 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 instance 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

Figure 1.5 – Setting a new master password

  1. Type in a complex 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 dialog box that opens, type the previous master password and the new one, and then click Continue:

Figure 1.6 – Changing the master password

Figure 1.6 – Changing the master password

Note

The master password is the server configuration file under the admin_passwd key. If the server is started without a configuration file being specified, 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. Follow these steps:

  1. On the database management screen, 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.7 – The Create Database dialog

Figure 1.7 – The Create Database dialog

  1. 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 your phone number (optional).
    • Language: Select the language that 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 the company’s 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 automated tests for the modules (refer to Chapter 7, Debugging Modules), you need to have the demonstration data since the vast majority of the automated tests in Odoo depend on these records to run successfully.

  1. Click Continue and wait until the new database is initialized. After, you will 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 filter option. 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 Installing Odoo from the source recipe. If you have a configuration file (refer to the Storing the instance configuration in a file recipe later in this chapter), 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. On the database management screen, click on the Duplicate Database link next to the name of the database you wish to clone:
Figure 1.8 – The Duplicate Database dialog

Figure 1.8 – The Duplicate Database dialog

  1. 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
  2. Click Continue.
  3. You can then click on the name of the newly created database on 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. On the database management screen, you will find the Delete button next to the name of the database. Clicking on it will bring up the following dialog:
Figure 1.9 – The Delete Database dialog

Figure 1.9 – The Delete Database dialog

  1. Fill in the form, as well as the Master Password field, which is the master password of the Odoo server.
  2. Click Delete.

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. On the database management screen, you will find the Backup button next to the database’s name. Clicking on it will bring up the following dialog:
Figure 1.10 – The Backup Database dialog

Figure 1.10 – The Backup Database dialog

  1. 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 since this is the only real full backup format. Only use the pg_dump format for a development database when you don’t care about the file store.
  2. Click Backup. The backup file will be downloaded to your browser.

Restoring a database backup

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

  1. On the database management screen, you will find a Restore Database button at the bottom of the screen. Clicking on it will bring up the following dialog:
Figure 1.11 – The Restore Database dialog

Figure 1.11 – The Restore Database dialog

  1. 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.
  2. Click Continue.

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. 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 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 you to download the backup file and upload 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 so that it doesn’t keep these in the database; this is the default option in 14.0. Unless you change it, these files reside in ~/.local/share/Odoo/filestore.

If the backup becomes too large, downloading it may fail. This will be 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’s 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 bonus of using this command line is that you can request add-ons to be installed while you are using it – 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 how to remember to set them properly when starting the server. Fortunately, it is possible to store them all in a configuration file and only specify the ones you want to alter for development, for example.

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 into the configuration form, use the long option name, remove the leading dashes, and convert the dashes in the middle into underscores. So, in this case, --without-demo will become without_demo. This works for most options, but there are a few exceptions, all of 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. After, 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 by looking at 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.1 – Difference in Odoo parameters regarding the command line and the configuration file

Table 1.1 – Difference in Odoo parameters regarding the command line and the configuration file

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

Table 1.2 – Odoo parameters and their usage

Table 1.2 – Odoo parameters and their usage

Here’s a list of configuration options related to the database:

Table 1.3 – Odoo parameters and their usage

Table 1.3 – Odoo parameters and their usage

The configuration file is parsed by Odoo 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.4 – Odoo parameters and their usage

Table 1.4 – Odoo parameters and their usage

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 advanced configuration options and fields. These options and fields are hidden in Odoo for better usability because they are not used daily.

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.12 – Links to activate different developer modes

Figure 1.12 – Links to activate different developer modes

  1. Click Activate the developer mode.
  2. Wait for the UI to reload.

Alternative way

It is also possible to activate 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 to use 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 doing this, you can toggle debug mode quickly without accessing the Settings menu. These extensions are available for Firefox and Chrome. The following screenshot shows one such plugin you can use and find in the Chrome store:

Figure 1.13 – Browser extension for debug mode

Figure 1.13 – Browser extension for debug mode

Note

The behavior of the debug mode has changed since Odoo v13. Since v13, the status of debug mode is stored in the 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 a list view. These provide 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 you 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 called Developer mode (with assets). This mode behaves like the normal developer mode, but the JavaScript and CSS code that’s sent to the browser is not minified, which means that the web development tools of your browser can easily be used to debug the JavaScript code (more on this in Chapter 15, Web Client Development).

Caution!

Test your add-ons both with and without developer mode since 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 a new add-on module is added, you need to run the Update Module List wizard to get your new application in the app list. In this recipe, you will learn how to update the app list.

Getting ready

Start your instance and connect to it using your Administrator account. After doing this, activate developer mode (if you don’t know how to activate developer mode, refer to the Activating Odoo developer tools recipe).

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 Update Apps List:
Figure 1.14 – Update Apps List

Figure 1.14 – Update Apps List

  1. In the dialog that appears, click Update:
Figure 1.15 – Dialog to update the apps list

Figure 1.15 – Dialog to update the apps list

  1. 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-on 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 is 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 add 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’ve 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.

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Create web client components using the latest features in the Odoo Web Library
  • Discover new widgets for display screens, fields, and attributes in Odoo 17
  • Build apps and websites with confidence with the help of this trusted community edition
  • Purchase of the print or Kindle book includes a free PDF eBook

Description

The latest version of the powerful Odoo framework uncovers a wide variety of features for rapid application development. Written by seasoned Odoo experts, this updated Odoo Development Cookbook will help you explore Odoo 17’s new features and use them to develop applications from scratch. You'll learn about website development concepts and get to grips with the Odoo Web Library (OWL), as well as create a new Odoo module from the ground up before progressing to advanced framework concepts. The book also shows you how to modify existing applications, including Point of Sale (POS) applications. Going beyond backend development, you'll discover advanced JavaScript recipes for creating new views and widgets. You'll get a clear understanding of website development and be on your way to becoming a proficient Odoo developer by studying performance optimization, debugging, and automated testing. By the end of this book, you'll have the knowledge you need to build impressive applications and be well-versed in Odoo development best practices.

Who is this book for?

This book is for developers who are new to the Odoo framework and want to develop effective business applications. Experienced Odoo developers looking to revisit specific topics or discover new features in Odoo 17 may also benefit from this book. Basic knowledge of Python programming and JavaScript programming is necessary to get the most out of this book.

What you will learn

  • Build intuitive websites with Odoo CMS using dynamic building blocks
  • Get to grips with advanced concepts such as caching, prefetching, and debugging
  • Modify backend JavaScript components and POS applications with the updated OWL framework
  • Connect and access any object in Odoo via Remote Procedure Calls (RPCs)
  • Manage, deploy, and test an Odoo instance with Odoo.sh
  • Configure IoT Box to add and upgrade Point of Sale (POS) hardware
  • Find out how to implement in-app purchase services

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : May 31, 2024
Length: 774 pages
Edition : 5th
Language : English
ISBN-13 : 9781805129479
Languages :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want

Product Details

Publication date : May 31, 2024
Length: 774 pages
Edition : 5th
Language : English
ISBN-13 : 9781805129479
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 158.97
Odoo Development Cookbook
$49.99
Mastering PostgreSQL 15
$61.99
Odoo 15 Development Essentials
$46.99
Total $ 158.97 Stars icon

Table of Contents

27 Chapters
Chapter 1: Installing the Odoo Development Environment Chevron down icon Chevron up icon
Chapter 2: Managing Odoo Server Instances Chevron down icon Chevron up icon
Chapter 3: Creating Odoo Add-On Modules Chevron down icon Chevron up icon
Chapter 4: Application Models Chevron down icon Chevron up icon
Chapter 5: Basic Server-Side Development Chevron down icon Chevron up icon
Chapter 6: Managing Module Data Chevron down icon Chevron up icon
Chapter 7: Debugging Modules Chevron down icon Chevron up icon
Chapter 8: Advanced Server-Side Development Techniques Chevron down icon Chevron up icon
Chapter 9: Backend Views Chevron down icon Chevron up icon
Chapter 10: Security Access Chevron down icon Chevron up icon
Chapter 11: Internationalization Chevron down icon Chevron up icon
Chapter 12: Automation, Workflows, Emails, and Printing Chevron down icon Chevron up icon
Chapter 13: Web Server Development Chevron down icon Chevron up icon
Chapter 14: CMS Website Development Chevron down icon Chevron up icon
Chapter 15: Web Client Development Chevron down icon Chevron up icon
Chapter 16: The Odoo Web Library (OWL) Chevron down icon Chevron up icon
Chapter 17: In-App Purchasing with Odoo Chevron down icon Chevron up icon
Chapter 18: Automated Test Cases Chevron down icon Chevron up icon
Chapter 19: Managing, Deploying, and Testing with Odoo.sh Chevron down icon Chevron up icon
Chapter 20: Remote Procedure Calls in Odoo Chevron down icon Chevron up icon
Chapter 21: Performance Optimization Chevron down icon Chevron up icon
Chapter 22: Point of Sale Chevron down icon Chevron up icon
Chapter 23: Managing Emails in Odoo Chevron down icon Chevron up icon
Chapter 24: Managing the IoT Box Chevron down icon Chevron up icon
Chapter 25: Web Studio Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon