How to do it...

Perform the following steps to create, write, and update a book's information through RPC:

  1. Add the books_operation.py file. You can place this file anywhere you want because the RPC program will work independently.
  2. 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:
# create new books
create_data = [
{'name': 'Book 1', 'release_date': '2019-01-26'},
{'name': 'Book 3', 'release_date': '2019-02-12'},
{'name': 'Book 3', 'release_date': '2019-05-08'},
{'name': 'Book 7', 'release_date': '2019-05-14'}
]
books_ids = models.execute_kw(db_name, user_id, password,
'library.book', 'create',
[create_data])
print("Books created:", books_ids)

# Write in existing book record
book_to_write = books_ids[1] # We will use ids of recently created books
write_data = {'name': 'Books 2'}
written = models.execute_kw(db_name, user_id, password,
'library.book', 'write',
[book_to_write, write_data])
print("Books written:", written)

# Delete the book record
books_to_delete = books_ids[2:] # We will use ids of recently created books
deleted = models.execute_kw(db_name, user_id, password,
'library.book', 'unlink',
[books_to_delete])
print('Books unlinked:', deleted)

else:
print('Wrong credentials')
  1. Run the Python script form the Terminal with the given command:
python3 books_operation.py

The preceding program will create four records of the books. Updating the data in the book records and later deleting two records gives you the following output (the IDs created may be different based on your database):

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset