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:
1 |
sudo pip install erppeek |
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:
1 |
sudo nano db_configurer.py |
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:
1 |
import erppeek |
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:
1 2 3 4 |
database = 'YourDatabase' server = 'http://localhost:8069' admin_password = 'admin' user = 'admin' |
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:
1 2 |
# Connect to the database client = erppeek.Client(server, database, user, admin_password) |
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:
1 |
config_id = client.model('res.config.settings').search([], limit=1, order='id desc') |
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:
1 2 3 4 5 6 7 |
if config_id: # This means there is already a configuration record - let us write on it config_record = client.model('res.config.settings').browse(config_id[0]) config_record.write({'group_uom': True}) # Execute the record in order to trigger the save and to apply the values config_record.execute() |
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:
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:
1 2 3 4 5 |
else: # This means there is no configuration yet - let us make a new record config_record = client.model('res.config.settings').create({}) config_record.write({'group_uom': True}) config_record.execute() |
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:
1 |
python db_configurer.py |
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:
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!