How to Write into Odoo Database from External Applications

Abid Patel
05-Nov-2024 Updated : 05-Nov-2024

Learn how to use Odoo's External API to write and update records in the Odoo database from external applications using Python.

How to Write into Odoo Database from External Applications

When you work with Odoo, there is a potent function. It is the External API. This feature lets you interact with Odoo database in a programmatic manner. It is vital for integrating Odoo with external applications. It grants the ability to update existing records seamlessly. The blog will delve into how to use the Odoo External API. It is for writing or updating records directly in the Odoo database.

Comprehending Odoo External API

Odoo provides robust API. This API supports XML-RPC and JSON-RPC protocols. These protocols enable developers to perform operations. Operations like creating reading and writing records. In this context focus will be on the write method. It allows modification of existing records in Odoo.

Steps to Write Data into Odoo

To write data into Odoo follow these steps:

  • ▹ Set Up Odoo API Access. Ensure you have access to Odoo API. Have necessary credentials. These include server URL, database name, username and password.
  • ▹ Choose Your Protocol. Decide on using XML-RPC or JSON-RPC. This is based on application's requirements.
  • ▹ Prepare data. Identify the model to update. Prepare data in dictionary format. You need record ID of record to update.
  • ▹ Make the API Call. Use write method to update record.

Here's an example of how to write data into existing partner record in Odoo. You can use Python with XML-RPC protocol.

python

import xmlrpc.client

url = 'http://localhost:8069'
db = 'your_database'
username = 'your_username'
password = 'your_password'

# Initialize the XML-RPC connection
common = xmlrpc.client.ServerProxy('{}/xmlrpc/2/common'.format(url))
uid = common.authenticate(db, username, password, {})

models = xmlrpc.client.ServerProxy('{}/xmlrpc/2/object'.format(url))

# Specify the ID of the partner you want to update
partner_id = 1  # Example ID of the partner record

# Prepare the data to update
update_data = {
    'name': 'Updated Partner Name',
    'email': 'updated_email@example.com',
    'phone': '0987654321',
}

# Write the updated data to the partner record
result = models.execute_kw(db, uid, password, 'res.partner', 'write', [[partner_id], update_data])

print(f"Update successful: {result}")

Code Explanation

  • ▹ Starting Connection: The script initiates connection to Odoo server. It uses XML-RPC.
  • ▹ Authentication: There is use of authenticate method. This is for log in. It is also for retrieval of the user ID (uid). This ID is necessary for further operations.
  • ▹ Identifying Record to Update: There is specification of partner_id. This corresponds to ID of the record we need to update.
  • ▹ Preparing Update Data: A dictionary gets created. It contains the fields we want to update.
  • ▹ Executing Write Method: There is use of write method. The method gets called on res.partner model. We pass the record ID and the updated data.

Conclusion

Integration with Odoo via External API serves as potent way of managing business data. The write method enables update of existing records. This update can directly come from external applications. This ensures that Odoo database stays synchronized with other systems.

Whether you're updating customer data or changing product details, applying the API can streamline your processes. It is a way to improve the efficiency.

Understanding this knowledge you can boost application capabilities. This allows for dynamic manipulation of data within Odoo. Happy coding!

Make a Comment

Your email address will not be published. Required fields are marked *