Configuring app settings with erppeek in Odoo 11

In this tutorial I will teach you how to automatically configure app settings with erppeek in Odoo 11. You will learn how to use the erppeek library and how to configure app settings from Python code. By doing so you can automate the configuration of your database(s).
In this tutorial I will configure the sales app to use different units of measure through Python code.
Tip: This tutorial will only work for Odoo 11. The concept will work for Odoo 8, 9 and 10 too but the code will be slightly different.

1. Installing erppeek

Before we can start using erppeek we will need to install it. Open up a Linux terminal and run the following command:

After running this command we can access and use erppeek.

2. Creating the Python script

Now create a new Python file on your system where you would like to save your new script:

2.1 Configuring the settings

Now that we have a new file we have to import erppeek, so we can access all the possibilities of the erppeek library:

The next step is to tell erppeek where it can find the database and how it can access it. In order to do this we will have to tell the library the database name, the location of the database, the username and the password. Add the following code to your script, under the import:

Tip: If you can you should use this script with the administrator user only, since this user has access to all models and settings.

2.2 Connecting to the database

So now that we’ve installed erppeek, we’ve imported the library and we’ve created the configuration the next step is to connect to the Odoo database in order to query and configure it. Just connect to the Odoo client:

This line will tell your script to which server, on which database we want to connect with what user and sets the user his password.
Thanks to this code we can now access the database and query it. We now just have to find the record that contains settings of this database and then do the actions we would like to execute.

2.3 Getting the Odoo configuration

In Odoo 11 all settings are saved in the model ‘res.config.settings’ so let us get data from this model:

So, what exactly does this code do? It tells erppeek that we want to get data from the model ‘res.config.settings’ and that we only want to get the last record from this model (limit=1, order=’id desc’ will do that).
The result will now be in the variable ‘config_id’ but there is a catch. What if this query didn’t return anything? Imagine that you have a database that you’ve just installed and you’ve never done any settings, in this case the variable config_id won’t contain any records. To prevent our script from crashing we should make an if/else statement now that handles this potential issue. Let us start with the if statement!

2.4 Setting the new configuration

If we find a result we should set the changes that we want to activate and we then have to apply them on the database. Have a look at this code:

So, what exactly does this do? The first line will get the record itself, as ‘config_id’ only contains the id for the record but not the record itself. By calling the Odoo database and using the ‘browse’ function (with the ‘config_id’ passed in the call) we will get the record back. When we have this record we can write our wanted changes on this record. In this case I will configure Odoo to use different units of measures. Remember that all settings in Odoo are booleans, so we do {‘group_uom’: True} to set the boolean to true. But, where did I get the ‘group_uom’ from? This is the field of this setting. You can get this by activating the developer mode and hovering over the field:
Field name configuration
In this screenshot you can see that the option is not yet activated, so the boolean is set to False. {‘group_uom’: True} will change this when we apply this value. This is exactly what the last line does! By calling the record its execute function we will tell Odoo to execute this change and apply it to Odoo.
If you would run this script and would go to the settings you would see that the units of measures is now activated.

2.5 Setting the configuration when there is no record yet

Now we should handle one more case. When there has never been any configuration record yet we should create one. Just have a look at the following code:

The code is almost identical to the if part (in chapter 2.4) with the minor change that we create a new and empty record with .create({}) in order to create new settings. Besides of this difference the rest of the code is identical to the if part of the code.

2.6 Running the file

Good job! You’re almost done. Now save your Python file and execute the python script to apply the setting(s) on your Odoo database. Execute the following command:

After a few seconds your script will be done. Now open up your Odoo, go to the configuration and you will see that the setting has been applied to your database:
Applied configuration
That’s it! You’ve now configured your own Odoo database settings with a simple Python script!

3. Conclusion

Thanks to the erppeek library you can easily configure app settings from a Python script. With this library you can automate your databases its settings in a matter of seconds. If you would like to learn more about erppeek and the options it offers you should have a look at the official documentation.
Do you want to try the demo code and see the source code of this tutorial? You can view it on my Github account.
Has this tutorial helped you, do you have any feedback or questions? Post away!
Tutorial sponsored by Oocademy


Override create functions in Odoo

In this tutorial I will teach you how to override the default create function from Odoo. You will learn how to override the create function and how to change values on a record before storing it in the database.
In this tutorial I will use the ‘res.partner’ model, which is the model from where you can create and edit contacts. I will override this create function, print a message in the terminal and will change the value of a field before storing the record in the database.

1. Inheriting the model

The first thing that you need to do is create a new python file in the ‘models’ folder. Open the ‘models’ folder of your custom module and create a new Python file named ‘’. After creating this file don’t forget to import it in ‘’.
Now, open up the ‘’ file, import Odoo, add a class name and inherit the ‘res.partner’ model. Your code should now look like this:

2. Override the create function

Now that we have access to all Odoo methods (thanks to the imports) and now that Odoo knows which model we want to inherit we can start inheriting the create function.
The create function is a default function from Odoo so we can use the API that is provided by Odoo, this is where we use @api.model. After calling @api.model we need to define a function name and pass self and values to the function:

At this point Odoo knows we are doing these operations on the model ‘res.partner’ and that we created a new function with the decorator @api.model.
Tip: Want more information about the Odoo API and the model decorator? See the official documentation
At this point Odoo doesn’t know that we want to inherit an already existing function though. This is where the super call comes in action. With super we can call the original function and override it to change the default behaviour. If you’d like more information about using super see the official Python documentation.
Now, call the super method, pass your class along and tell Python which function you want to override:

So, what exactly does this code do? It tells Python to call the original create function for the model ‘res.partner’ and to apply all the current values to the variable ‘record’.  the variable ‘values’ contains all the values and is passed in the .create() function so that all variables are in ‘record’. In the variable ‘record’ will be all values that you’ve filled in on the form up untill the point you clicked save.

2.1 Changing values in the super

Now, let us go a step further, and add a new field to the model ‘res.partner’ to change a variable the moment we override this function.
Add the following field to your model:

Usually when you override a default function you want to change the value of a field or apply a value. In this example I will set the just created field ‘passed_override_write_function’ to true so that this value is also stored in the database. You can get, or change, any value on this record by calling record[‘field_name’] to change it. Set the variable ‘passed_override_write_function’ to true every time we pass this function:

2.2 Returning the super call

The final step is to return the record and stop the function. Without this your function will not work and the new values will not be returned. Returning the record is very simple:

Let us have a look at the full code. Your ‘’ file should now look like this:

Congratulations, that is all! You’ve now overriden the create method, changed a value in it and stored it in the database. Now create a new contact and click on ‘Save’. The result in the terminal:New contact creation

The result in the database:Database result after override

3. Conclusion

Overriding existing functions in Odoo is quite easy to do. Odoo is built on Python and Python allows you to use super calls, which makes it quite easy to override existing functions.

Do you want to try the demo code and see the source code of this tutorial? You can view it on my Github account.
Has this tutorial helped you, do you have any feedback or questions? Post away!
Tutorial sponsored by Oocademy