How to do it...

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

  1. Add the books_method.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 book with state draft
book_id = models.execute_kw(db_name, user_id, password,
'library.book', 'create',
[{'name': 'New Book', 'date_release': '2019-01-26', 'state': 'draft'}])

# Call make_available method on new book
models.execute_kw(db_name, user_id, password,
'library.book', 'make_available',
[[book_id]])

# check book status after method call
book_data = models.execute_kw(db_name, user_id, password,
'library.book', 'read',
[[book_id], ['name', 'state']])
print('Book state after method call:', book_data[0]['state'])
else:
print('Wrong credentials')
  1. Run the Python script from the terminal with the following command:
python3 books_method.py

The preceding program will create one book with draft and then we will change book state by calling the make_available method. After that, we will fetch the book data to check the book's status, which will give the following output:

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

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