Perform the following steps to fetch a book's information through RPC:
- Add the books_data.py file. You can place this file anywhere you want to because the RPC program will work independently.
- Add the following code to the file:
from xmlrpc import client
server_url = 'http://localhost:8069'
db_name = 'test-12'
username = 'admin'
password = 'admin'
common = client.ServerProxy('%s/xmlrpc/2/common' % server_url)
user_id = common.authenticate(db_name, username, password, {})
models = client.ServerProxy('%s/xmlrpc/2/object' % server_url)
if user_id:
search_domain = ['|', ['name', 'ilike', 'odoo'], ['name', 'ilike', 'sql']]
books_ids = models.execute_kw(db_name, user_id, password,
'library.book', 'search',
[search_domain],
{'limit': 5})
print('Books ids found:', books_ids)
books_data = models.execute_kw(db_name, user_id, password,
'library.book', 'read',
[books_ids, ['name', 'date_release']])
print("Books data:", books_data)
else:
print('Wrong credentials')
- Run the Python script form the Terminal with the following command:
python3 books_data.py
The preceding program will fetch the book data and give you the following output (the output may be different based on your data):