Perform the following steps to create, write, and update a book's information through RPC:
- Add the jsonrpc_method.py file. You can place this file anywhere you want because the RPC program will work independently.
- Add the following code to the file:
# place authentication and get_json_payload method (see last recipe for more)
if user_id:
# Create the book in draft state
payload = get_json_payload("object", "execute_kw",
db_name, user_id, password,
'library.book', 'create', [{'name': 'Book 1', 'state': 'draft'}])
res = requests.post(json_endpoint, data=payload, headers=headers).json()
print("Has create access:", res['result'])
book_id = res['result']
# Change the book state by calling make_available method
payload = get_json_payload("object", "execute_kw",
db_name, user_id, password,
'library.book', 'make_available', [book_id])
res = requests.post(json_endpoint, data=payload, headers=headers).json()
# Check the book status after method call
payload = get_json_payload("object", "execute_kw",
db_name, user_id, password,
'library.book', 'read', [book_id, ['name', 'state']])
res = requests.post(json_endpoint, data=payload, headers=headers).json()
print("Book state after the method call:", res['result'])
else:
print("Failed: wrong credentials")
- Run the Python script from the terminal with the following command:
python3 jsonrpc_method.py
The preceding command will create one book using draft and then we will change the book state by calling the make_available method. After that, we will fetch book data to check the book status, which will give the following output: