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 |
sudo pip install erppeek |
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:
1 2 3 4 5 |
import erppeek DATABASE = 'ErpPeekDemoDatabase' SERVER = 'http://localhost:8080' ADMIN_PASSWORD = 'admin' |
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:
1 |
client = erppeek.Client(server=SERVER) |
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:
1 2 3 4 5 |
if not DATABASE in client.db.list(): print("The database does not exist yet, creating one!") client.create_database(ADMIN_PASSWORD, DATABASE) else: print("The database " + DATABASE + " already exists.") |
That is all you need to do! Just to be clear, here is whole code:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import erppeek DATABASE = 'ErpPeekDemoDatabase' SERVER = 'http://localhost:8080' ADMIN_PASSWORD = 'admin' client = erppeek.Client(server=SERVER) if not DATABASE in client.db.list(): print("The database does not exist yet, creating one!") client.create_database(ADMIN_PASSWORD, DATABASE) else: print("The database " + DATABASE + " already exists.") |
When you now execute this Python file it will create the new database for you. Save the Python file and execute it:
1 |
python create_database.py |
After a few seconds you will see the following result in your terminal:
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:
1 2 3 4 |
import erppeek client = erppeek.Client('http://localhost:8080', 'ErpPeekDemoDatabase', 'admin', 'admin') |
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:
1 2 3 |
proxy = client.model('ir.module.module') installed_modules = proxy.browse([('state', '=', 'installed')]) |
And for demo purposes let us simply print out this data in the terminal:
1 2 3 |
for module in installed_modules: print('Installed module: ' + module.name) |
When you now execute this Python file it will it will print out all installed modules. Save the Python file and execute it:
1 |
python list_installed_modules.py |
After a few seconds you will see the following result in your terminal:
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:
1 2 3 |
import erppeek client = erppeek.Client('http://localhost:8080', 'ErpPeekDemoDatabase', 'admin', 'admin') |
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:
1 2 3 4 5 |
modules = client.modules('sale', installed=False) if 'sale' in modules['uninstalled']: client.install('sale') print('The module sale has been installed!') |
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:
1 |
python install_module.py |
After a few seconds you will see the following result in your terminal:
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import xmlrpclib username = 'admin' # The Odoo user pwd = 'admin'# The password of the Odoo user dbname = 'ErpPeekDemoDatabase' # The Odoo database # OpenERP Common login Service proxy object sock_common = xmlrpclib.ServerProxy ('http://localhost:8080/xmlrpc/common') uid = sock_common.login(dbname, username, pwd) # replace localhost with the address of the server if it is not on the same server sock = xmlrpclib.ServerProxy('http://localhost:8080/xmlrpc/object') |
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:
1 2 3 4 |
partner_record = { 'name': 'Fabien', 'email': 'example@odoo.com' } |
The last part is to write the new record to Odoo (with XML-RPC) by using sock.execute. The code:
1 2 3 |
# Calling the remote ORM create method to create a record result = sock.execute(dbname, uid, pwd, 'res.partner', 'create', partner_record) print('Inserted new partner! Id: ' + str(result)) |
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:
1 |
python create_contact.py |
After a second or two you will see the following result in your terminal:
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:
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import xmlrpclib username = 'admin' # The Odoo user pwd = 'admin'# The password of the Odoo user dbname = 'ErpPeekDemoDatabase' # The Odoo database # OpenERP Common login Service proxy object sock_common = xmlrpclib.ServerProxy ('http://localhost:8080/xmlrpc/common') uid = sock_common.login(dbname, username, pwd) # replace localhost with the address of the server if it is not on the same server sock = xmlrpclib.ServerProxy('http://localhost:8080/xmlrpc/object') |
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:
1 2 3 |
# Calling the remote ORM create method to search for records contact_ids = sock.execute(dbname, uid, pwd, 'res.partner', 'search', []) print('Contact IDs found: ' + str(contact_ids)) |
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:
1 2 3 4 5 |
# Get the details from all contacts for contact_id in contact_ids: # Get the details by the id contact = sock.execute(dbname, uid, pwd, 'res.partner', 'read', contact_id, []) print('Contact name: ' + contact['name']) |
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:
1 |
python search_contact.py |
After a second or two you will see the following result in your terminal:
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:
1 2 3 4 |
import erppeek client = erppeek.Client('http://localhost:8080', 'ErpPeekDemoDatabase', 'admin', 'admin') |
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:
1 2 |
model_proxy = client.model('ir.model') field_proxy = client.model('ir.model.fields') |
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:
1 2 3 4 5 6 7 8 9 |
values = { 'model': 'x_example_tutorial', 'name': 'Example model', 'state': 'manual', } # This will create the new model! model = model_proxy.create(values) |
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:
1 2 3 4 5 6 7 8 9 10 11 12 |
values = { 'name': 'x_name', 'ttype': 'char', 'size': 64, 'field_description': 'Firstname', 'model_id': model.id, 'model': model.model, 'domain': '[]', } field = field_proxy.create(values) print('Congratulations your new model has been created!') |
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!