Create and manage Odoo databases with ERPpeek or XML-RPC

Hi guys,

In this tutorial I will learn you how to connect to your Odoo database with ERPpeek or with simple XML-RPC calls. In this tutorial you will learn how to create new databases, how to install modules, how to add models and how to search for data in the database or write data in the database. These XML-RPC calls can be done from anywhere, also remotely.

In this example I will learn you how to create a new database, how to list the installed modules, how to install a module, how to add models and eventually how to search for data and add data in the database!
In this example I will be working with two ways: first with ERPpeek (which is an XML-RPC library wrapper which makes coding even easier) and I will then switch to XML-RPC calls, which are slightly more complex but do not need any remote libraries installed.

1. Installing ERPpeek

The first thing that you will need for this library is the ERPpeek library, which you can install very easily:

1.1 Creating a new database

One of the handy methods in ERPpeek is that you can very easily create a new database by command in a Python file. In this blogpost I will be separating all examples completely so I will also add the connection code again every time. This is because I will be switching from Erppeek to normal XML-RPC calls later on, to learn you both concepts.
Alright let us start! Create a new folder somewhere where you can create new Python files in. Now create a new Python file named ‘create_database.py’. So, what do we need to create a new database? A database name, the path to the server (either on the same server or remote) and an administrator password for the admin user. A second thing that we need is to import the Python library ERPpeek, so we can use all the handy tools from there. The code will look like this:

So, we now have the credentials but how do we create the database? We can access all functions with ‘erppeek.client’ (you can find all methods in the ERPpeek documentation) which opens up a broad range of options. The first thing we need to do is create a client:

So we have the credentials now, we have a client and now we need to create the database! From here on we can acces everything we need from the variable ‘client’. Let us first add a validation to see that the database doesn’t exist yet and then create a new database with the function ‘create_database’! The code:

That is all you need to do! Just to be clear, here is whole code:

When you now execute this Python file it will create the new database for you. Save the Python file and execute it:

After a few seconds you will see the following result in your terminal:
New created database with ERPpeek

Congratulations, you’ve just created your first Odoo database by command. Now let us continue and see which modules are already installed.

1.2 Getting installed modules

A handy thing to know is which modules are installed in your Odoo, so let us create a new Python file named ‘list_installed_modules.py’. Import ERPpeek and create a new client to connect with:

The first value for the erppeek.Client is the server IP (or localhost, which translates to 0.0.0.0) and the port where the Odoo runs on, in this case 8080. The second value is the name of your database (ErpPeekDemoDatabase), the third is your Odoo username (admin) and the fourth is the user its password (admin).
So, we now have a working connection to the Odoo database and we now need to find out which modules are installed. This data is saved in the model ‘ir.module.data’, when the field ‘state’ is set to installed we know the module is installed. This translates to the following code with ERPpeek:

And for demo purposes let us simply print out this data in the terminal:

When you now execute this Python file it will it will print out all installed modules. Save the Python file and execute it:

After a few seconds you will see the following result in your terminal:
Installed modules

We now know which modules are installed, but wouldn’t it be great if we could install modules by command? Read on in chapter 1.3!

1.3 Installing a module

Create a new Python file and name it ‘install_module.py’. Import ERPpeek and create a client just like you did in the previous examples:

We can now get all modules with ‘client.Modules’ and we can check if it is installed with the setting installed=False. When the module is in the state uninstalled we can install it with client.install. In this example I will check for the module sale and if it isn’t installed I will install it. The code:

When you now execute this Python file it will install the module for you (if it isn’t installed yet). Save the Python file and execute it:

After a few seconds you will see the following result in your terminal:
New created database
Do you notice anything interesting? Odoo will automatically check all dependencies and if some aren’t met they will be installed by Odoo! So we have now created a new database, we’ve checked which modules are installed and installed one which wasn’t installed yet. Now let us continue by adding a new contact to the ‘res.partner’ model.

1.4 Adding new records to a model with XML-RPC

So we’ve seen a few examples with ERPpeek now but we do not really need this library. We can just as easy do simple XML-rpc calls! It involves a bit more coding but it gives you the same possibilities as with the ERPpeek library. From here on I will continue this tutorial with XML-RPC calls for learning purposes. Do know that you can also use the ERPpeek library if you’d like to!

So we no longer need the ERPpeek library but we now need the library ‘xmlrpclib’. Import it and create a connection to the Odoo environment. We first need a ServerProxy which refers to the URL ‘localhost:xxxx/xmlrpc/common’ we then need an uid and then we need a socket to connect to. The code will look like this:

Do you see the similarities between the previous examples where we instantiated a client in one line? The working method is in essence the same, the code just looks slightly different and it is a bit more complex. What we did in one line in the previous examples now takes 6 lines but there is no difference in the possibilities.
We now have a client to connect with Odoo so let us now create a new record. The new record should be written in a dictionary format where you can add values for every field you would like to fill. For example I will create a new contact on the model ‘res.partner’ with the name ‘Fabien’ and the e-mail ‘example@odoo.com’. This translates in the following code:

The last part is to write the new record to Odoo (with XML-RPC) by using sock.execute. The code:

What does this code do? It connects to the database with the credentials you’ve provided at the top of your script, it calls the model ‘res.partner’, uses the method ‘create’ (which creates a new record) and eventually we pass the dictionary to Odoo to write it in the database.

When you now execute this Python file it will create a new record in the database, on the model ‘res.partner’. Save the Python file and execute it:

After a second or two you will see the following result in your terminal:
New inserted contact
The new contact has been created with the id 7. When you now open up your Odoo and go to sales > contacts you will see the new contact:
New contact
Congratulations, you can now also connect with Odoo through XML-RPC calls and you can write data in the Odoo database! Now, let us learn how to fetch data from the database.

1.5 Searching for data in the database

Create a new Python file named ‘search_contact.py’, import the xmlrpclib, create credentials and create a socket connection:

Create a new sock.execute, just like you did in the previous example, but this time use the search method on the ‘res.partner’ model to get all contacts:

When we would execute the code at this point it would print us all the ID’s from all records on the model ‘res.partner’, but we as humans don’t know a lot by ids so let us translate these ids in to readable content! Loop over all the ids and create another sock.execute with the read function to get all the details. Finally print out the details of all contacts:

When you now execute this Python file it will search all records in the database, on the model ‘res.partner’. Save the Python file and execute it:

After a second or two you will see the following result in your terminal:
Details from inserted contacts

Congratulations! You can now also search for data in Odoo with XML-RPC!
There is one more thing I would like to learn you: how to create new models through XML-RPC calls.

1.6 Creating new models with XML-RPC calls

A rather often needed thing in Odoo is to create new models and this can also be done through XML-RPC calls! There is one downside though: you will need to create the model and field names starting with x_. This is not the case when you create a new model in a new module so it is probably better to create models that way.
In case you should do it from somewhere remote you can do it with calls though! This could be done with the XML-RPC library but we would need a lot more code so for this example I’m going back to ERPpeek!
Create a new Python file named ‘create_new_model.py’, import ERPpeek and create a client:

In order to create a new model you will need access to the models ‘ir.model’ and ‘ir.model.fields’ so create two variables for this:

The first step is to create a new model, which will be empty, and after this is done you can add fields to it. First create a new model:

So what does this do? It creates a new model with the name ‘x_example_tutorial’ and the text will be ‘Example’ model. The model_proxy.create will finally create the new model.
Now that we have a new model the last thing we need to do is add fields to the model. Create a new dictionary with the field and all settings you want to specify:

This will create a new field with the name ‘x_name’, the type is a char with a length of 64 characters, the description of the field is ‘Firstname’ and we will simply add it on the just created model. field_proxy.create will eventually insert the new field in the database!

Thats it, congratulations! You now know quite a lot about XML-RPC calls and ERPpeek to access Odoo databases.

2. Conclusion

Using simple XML-RPC calls is a very powerfull tool to access and manage your Odoo database but when you use the ERPpeek library life gets even more easy. You can create new databases in a few lines of code, you can install modules and write to the database remotely.

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




PayPal

Odoo Experts

Installing Odoo 9 enterprise on Ubuntu 14.04

Hi guys,

1. Introduction

In this tutorial I will learn you how to install Odoo 9 Enterprise on Ubuntu 14.04. The script that you will use is based on the code from André Schenkels but has been updated, improved and I’ve added Enterprise support. Do notice that you will need to be an official partner or that you need to have bought the Enterprise subscription from Odoo or you will have no access to the Github repository for the Enterprise code!

2. Downloading the script

The first step is to download my script from Github and to add the code in a new .sh file on your Ubuntu machine, wherever you’d like this.
For example right under /home. Open up an Ubuntu terminal and cd to the directory where you’d like to keep the script and then create the file:

If you’re curious about how the whole code looks and works you can find it on my Github account.

Now open up the file and edit the parameters to your liking:

There are some things you can configure/change to your likings at the top of the script. You can choose if you wish to install Wkhtmltopdf or not, which version you’d like, where the location is and most importantly what the master admin password is. Tip: always modify this for every Odoo you install!

3. Making the Odoo installation file executable

The next step is to make this file executable. After you’ve made it executable you can execute it and everything will be installed automatically.
do this with the following command:

4.Running the script

Now that the code is in your file and the file is executable you simply have to execute it with the following command:

You will see that the script automatically starts updates, downloads required packages, creates the user, downloads the code from Github, … Eventually you will need to give in your Github credentials to download the Enterprise code (since this is a private repository). Fill in your details and let the script continue:
Odoo 9 Enterprise authentication
Give the script a few minutes to configure and install everything and eventually you will see something like this:
Result install script

You now have a fully functional Odoo V9 Enterprise on your system! Congratulations.
Odoo V9 Enterprise

5. Extra information about Odoo 9 Enterprise

Since Odoo Enterprise uses code from both http://github.com/odoo/odoo and github.com/odoo/enterprise we will separate the code with this script. This will make future upgrades easier and the code is nicely separated. This means that the default V9 code will be under /odoo/odoo-server/ and all the enterprise code will be under /odoo/enterprise/.

In the script you saw there was an option to change the Odoo port (OE_PORT). When you’d change this port number to 8070 in the install script it would be applied to /etc/your-config-file.conf and this would give you the ability to change the default port.
To apply these changes you should do the following:
Changing Odoo settings
The -c will change the configuration and memorize what you’ve changed under /etc/your-config-file.conf. Because my port was set to 8070 this is telling the Odoo that it should run on port 8070. When you would now open up your browser and navigate to http://localhost:8070/ you will see it is running there:
Odoo V9 alternative port

Has this tutorial helped you, do you have any feedback or questions? Post away!




PayPal

Odoo Experts